[LeetCode] 046: Longest Substring Without Repeating Characters
2017-09-10 20:49
260 查看
[Problem]
Given 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.
[Solution]
Given 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.
[Solution]
class Solution { public: int lengthOfLongestSubstring(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int res = 0, i = 0, j = 0; map<char, int> lastIndex; while(j < s.size()){ if(lastIndex.find(s[j]) != lastIndex.end() && lastIndex[s[j]] >= i){ i = lastIndex[s[j]] + 1; } res = max(res, j - i + 1); lastIndex[s[j]] = j; j++; } return res; } };说明:版权所有,转载请注明出处。Coder007的博客
相关文章推荐
- [LeetCode 3] Longest Substring Without Repeating Characters
- leetcode 3 -- Longest Substring Without Repeating Characters
- leetcode_java_Longest Substring Without Repeating Characters
- Leetcode-Longest Substring Without Repeating Characters
- leetcode: Longest Substring Without Repeating Characters
- [leetcode3] Longest Substring Without Repeating Characters
- LeetCode 3 Longest Substring Without Repeating Characters
- LeetCode算法问题7 —— Longest Substring Without Repeating Characters
- LeetCode Longest Substring Without Repeating Characters
- Leetcode 3 Longest Substring Without Repeating Characters
- LeetCode——Longest Substring Without Repeating Characters
- leetcode-Longest Substring Without Repeating Characters 最长不重复子串
- LeetCode3 Longest Substring Without Repeating Characters
- 【leetcode with java】3 Longest Substring Without Repeating Characters
- LeetCode Exercise 3: Longest Substring Without Repeating Characters
- Leetcode - Longest Substring Without Repeating Characters
- leetcode-Longest Substring Without Repeating Characters
- LeetCode|Longest Substring Without Repeating Characters
- leetcode: Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters