Given a string, find the length of the longest substring without repeating characters.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题目一拿到手,想都不用想,直接暴力遍历。然而结果虽然可行,但是效率太低,最终还有一个案例没通过,晕。还是要多动脑啊

public int lengthOfLongestSubstring(String s) {
int length = 0;
if(isUnique(s))
{
length = s.length();
}
int index = s.length();
for(int i = 0; i < index; i++)
{
for(int j = index; j >= i; j--)
{
String str = s.substring(i, j);
if(isUnique(str) && str.length() > length)
{
//从字符串的后面往前遍历,遇到第一个符合条件的肯定就是最长的之一,因此不用再往前遍历了
length = str.length();
break;
}
//剩下的子字符串的长度小于等于暂时的结果,也无需再往前遍历
if(j - i <= length)
{
break;
}
}
}
return length;
} public boolean isUnique(String s)
{
Set<Character> set = new HashSet<Character>();
char[] chars = s.toCharArray();
for(char c: chars)
{
set.add(c);
}
return (set.size() == s.length());
}

  无论怎么去优化,最终还是有一个案例没能通过。貌似暴力解法在此题行不通??

看到各种题解都提及到滑动窗口解法,于是去查了一下这个算法的思想,并根据自己的理解去写一下代码,终于通过了,虽然还有很大的提升空间...

    public int lengthOfLongestSubstring(String s) {
int result = 0;
int left = 0; //窗口左边界
int right = 0; //窗口右边界
int length = s.length();
Set<Character> set = new HashSet<Character>();
while(left < length && right < length)
{
int temp = 0;
if(!set.contains(s.charAt(right)))
{
set.add(s.charAt(right));
right++;
temp = set.size();
if(temp > result)
{
result = temp;
}
}
else
{
//如果发现set中已经存在此字符,就把左边界右移
set.remove(s.charAt(left));
left++;
}
}
return result;
}

length of the longest substring without repeating character的更多相关文章

  1. 3. Longest Substring Without Repeating Character[M] 最大不重复子串

    题目 Given a string, find the length of the longest substring without repeating characters. Example 1: ...

  2. [刷题] 3 Longest Substring Without Repeating Character

    要求 在一个字符串中寻找没有重复字母的最长子串 举例 输入:abcabcbb 输出:abc 细节 字符集?字母?数字+字母?ASCII? 大小写是否敏感? 思路 滑动窗口 如果当前窗口没有重复字母,j ...

  3. Leetcode 解题 Longest Substring without repeating charcater python

    原题: Given a string, find the length of the longest substring without repeating character For example ...

  4. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  5. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  6. No.003:Longest Substring Without Repeating Characters

    问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...

  7. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  8. No.003 Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...

  9. Java [leetcode 3] Longest Substring Without Repeating Characters

    问题描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

随机推荐

  1. UNIX环境高级编程 使用方法

    1.解压文件到apue.2e目录2.修改相应平台的文件,我使用的是linux,所以修改Make.defines.linux你修改的只需要这一行WKDIR=/home/your_dir/apue2e_s ...

  2. 第五篇 Flask 中内置的 Session

    Flask中的Session非常的奇怪,他会将你的SessionID存放在客户端的Cookie中,使用起来也非常的奇怪 1. Flask 中 session 是需要 secret_key 的 from ...

  3. Splash的使用

    Splash Lua脚本http://localhost:8050 入口及返回值 function main(splash, args) splash:go("http://www.baid ...

  4. DB2中的MQT优化机制详解和实践

    MQT :物化查询表.是以一次查询的结果为基础  定义创建的表(实表),以量取胜(特别是在百万,千万级别的量,效果更显著),可以更快的查询到我们需要的结果.MQT有两种类型,一种是系统维护的MQT , ...

  5. Asp.Net Core 单元测试正确姿势

    背景 ASP.NET Core 支持依赖关系注入 (DI) 软件设计模式,并且默认注入了很多服务,具体可以参考 官方文档, 相信只要使用过依赖注入框架的同学,都会对此有不同深入的理解,在此无需赘言. ...

  6. Parted 创建 GPT 分区

    对于磁盘的分区表 MBR与GPT区别. MBR:MBR分区表(即主引导记录)大家都很熟悉,是过去我们使用windows时常用的. 所支持的最大卷:2T,而且对分区有限制:最多4个主分区或3个主分区加一 ...

  7. CSPS_101

    T1 众所周知,只要在任意三个方向上有连续五颗棋子,游戏即结束. T2 又是最短路优化dp啦. T3 神奇的期望dp.还没改出来. 改出来啦!

  8. CSPS模拟 72

    状态..找不回来了.. T2 简单的期望 考试的时候忘考虑一个事,就是连续多位进位的情况 考试的时候打出$n^2$复杂度dp还没引起怀疑真是不应该. T3 简单的操作 最后一刻才想到图不联通,已经想不 ...

  9. NOIP模拟测试25

    这次考试后面心态爆炸了...发现刚了2h的T2是假的之后就扔掉了,草率地打了个骗分 T1只会搜索和m=0 最先做的T3,主要是发现部分分很多,当时第一眼看上去有87分(眼瞎了). 后来想了想,感觉一条 ...

  10. Linux下Redis安装与配置

    http://www.cnblogs.com/_popc/p/3684835.html