Leetcode 3 Longest Substring Without Repeating Characters
程序员文章站
2022-07-13 21:29:54
...
思路: 遍历所有substring,并判断是否是无重复字符串,然后取长度最长的一个。很简单,直接上代码:
class Solution {
private boolean isNoRepeatC(String s){
for(int i = 0; i < s.length(); i++){
if(s.lastIndexOf(s.charAt(i)) != i){return false;}
}
return true;
}
public int lengthOfLongestSubstring(String s) {
int res = 0;
for(int i = 0; i< s.length(); i++){
for(int j = i; j < s.length(); j++){
String str = s.substring(i,j+1);
if((j - i + 1) <= res){continue;}
if(!isNoRepeatC(str)){break;}
res = j - i + 1;
}
}
return res;
}
}
总结:
- 像这种暴力解法很容易,但是还有很多地方可以优化,建议复看的时候去看这题的标答
上一篇: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
下一篇: leetcode【3】Longest Substring Without Repeating Characters
推荐阅读
-
Longest Substring Without Repeating Characters
-
LeetCode - 3.Longest Substring Without Repeating Characters(388ms)
-
[LeetCode]3.Longest Substring Without Repeating Characters
-
Leetcode_3. Find the longest substring without repeating characters
-
3-Longest Substring Without Repeating Characters @LeetCode
-
Longest Substring Without Repeating Characters (leetcode 3)
-
【Leetcode 3】Longest Substring Without Repeating Characters
-
Leetcode 3 Longest Substring Without Repeating Characters
-
leetcode【3】Longest Substring Without Repeating Characters
-
LeetCode(3)Longest Substring Without Repeating Characters