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. ios发送短信验证码计时器的swift实现

    转载自:http://www.jianshu.com/p/024dd2d6e6e6# Update: Xcode 8.2.1 Swift 3 先介绍一下 属性观测器(Property Observer ...

  2. TOMACT 各个版本集合

    http://archive.apache.org/dist/tomcat/ 再点击 bin 目录,找到想下载的即可

  3. find 小案例

    说明:前几天对生产环境的一些重要数据进行备份时用到了find,查找特定符合条件的文件名后拷贝至指定目录,但是只拷贝了部分匹配到的文件 小案例模拟还原: [root@centos- ~]# ll /te ...

  4. UVA12433 【Rent a Car】

    这题应该算是比较难的一道网络流的题,(但却在我校OJ考试上出现了),但是大家只要能理解此图的建边方式就行. 假设有5天的租车需求,虚拟出2*n+2 即 12个节点,0为源点,12为汇点. 1,源点到1 ...

  5. 【长期维护】C++休闲(修仙)躲方块小游戏

    左右键控制小球左右移动,上键加速,Esc退出. 一个‘@’20分 #include <windows.h> #include <bits/stdc++.h> #include ...

  6. nginx篇最初级用法之地址重写

    nginx服务器的地址重写,主要用到的配置参数是rewrite rewrite regex replacement flag rewrite 旧地址 新地址 [选项] 支持的选项有: last 不再读 ...

  7. NOIP模拟 25

    分层考试第一场. 垫底. T1 lighthouse 观察到m很小,想到容斥. 正常人都想枚举子集,只有我打了搜索. 为了压行,我压缩了几句分类讨论. 压错了,原地爆炸 考场思路: 不容斥这也不可做啊 ...

  8. sshd服务以及基于口令的远程登陆

    ssh用为客户端,主要进行服务器端的连接:sshd用为服务器端 几个常用的命令: systemctl              ##服务控制命令   systemctl start sshd   ## ...

  9. 基于c/s架构的远程登陆服务的步骤。

    1:上/下位机安装相应的服务程序.(确保内核支持该服务)2:上位机(作为服务器端)配置能够给下位机访问目录的所在地,及其读写权限.3:在/dev目录下创建该服务其所需要使用的虚拟文件设备,同时按照该服 ...

  10. python函数的基本语法<一>

    函数: 一次定义,多次调用,函数可以变相看成变量函数的阶段: 1.定义阶段 2调用阶段 形参和实参: 定义阶段的参数叫形参,调用阶段的参数叫实参 函数的几种基本用法: #多变量 def test(na ...