LeetCode OJ : 3 Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters My Submissions Question
Total Accepted: 109306 Total Submissions: 524897 Difficulty: MediumGiven a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<string> substrs;
string str;
for(int i = 0; i < s.size(); ++i){
if(str.find(s[i]) == string::npos){
str.push_back(s[i]);
}
else{
substrs.push_back(str);
str.clear();
i = substrs.size()-1; //從這個重複的字元處開始再次向後查詢
}
if(i == s.size()-1){ //考慮到一個字串中的字元全部都不相同的情況
substrs.push_back(str);
}
}
int maxlen = 0;
for(int i = 0; i < substrs.size(); ++i){
if(substrs[i].size() > maxlen){
maxlen = substrs[i].size();
}
}
return maxlen;
}
};
這個思路很簡單,但是時間複雜度太高;
相關文章
- Leetcode 3 Longest Substring Without Repeating CharactersLeetCode
- Leetcode 3. Longest Substring Without Repeating CharactersLeetCode
- #3 Longest Substring Without Repeating Characters[M]
- 3. Longest Substring Without Repeating Characters
- Leetcode Longest Substring Without Repeating CharactersLeetCode
- Longest Substring Without Repeating Characters
- Leetcode javascript 3 longest-substring-without-repeating-charactersLeetCodeJavaScript
- Leetcode-Longest Substring Without Repeating CharactersLeetCode
- Longest Substring Without Repeating Characters leetcode javaLeetCodeJava
- [LeetCode] 3. Longest Substring Without Repeating Characters 題解LeetCode
- 149 Longest Substring Without Repeating Characters
- 【LeetCode從零單排】No 3 Longest Substring Without Repeating CharactersLeetCode
- LeetCode Longest Substring Without Repeating Characters(003)解法總結LeetCode
- LeetCode3:Longest Substring Without Repeating Characters(無重複字元的最長子串)LeetCode字元
- LeetCode-Longest Substring with At Least K Repeating CharactersLeetCodeAST
- [LeetCode] Longest Substring Without Repeating Characters 最長無重複字元的子串LeetCode字元
- Leet Code 3. Longest Substring Without Repeating Characters (最長的沒有重複字元的子字串)字元字串
- LeetCode OJ : 5 Longest Palindromic SubstringLeetCode
- LeetCode-Longest Substring with At Most K Distinct CharactersLeetCode
- Leetcode-Longest Substring with At Most Two Distinct Characters.LeetCode
- 【Leetcode】3. Longest Substring Without RepeatingCharacters無重最長子串LeetCodeGC
- Leetcode Longest Palindromic SubstringLeetCode
- LeetCode 5 (Longest Palindromic Substring)LeetCode
- Leetcode-Longest Palindromic SubstringLeetCode
- Longest Palindromic Substring leetcode javaLeetCodeJava
- LeetCode 5. Longest Palindromic SubstringLeetCode
- LeetCode每日一題:longest palindromic substringLeetCode每日一題
- [leetcode] 1624. Largest Substring Between Two Equal CharactersLeetCode
- [LeetCode] 2414. Length of the Longest Alphabetical Continuous SubstringLeetCodeAlphabet
- [LeetCode] Longest Palindromic Substring 最長迴文子串LeetCode
- Leetcode5: Longest Palindromic Substring(最長迴文子串)LeetCode
- LintCode-Longest Common Substring
- Leetcode Longest Common PrefixLeetCode
- [LeetCode]Longest Common PrefixLeetCode
- [LeetCode] 451. Sort Characters By FrequencyLeetCode
- LeetCode OJ : 1 Two SumLeetCode
- Leetcode 14 Longest Common PrefixLeetCode
- Leetcode 32 Longest Valid ParenthesesLeetCode