Leetcode[3] Longest Substring Without Repeating Characters
2015-05-07 21:41
351 查看
Given astring, 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.
C++版
is "b", with the length of 1.
C++版
class Solution { public: int lengthOfLongestSubstring(string s) { int n = s.length(); int i=0,j=0; ///两个指针,i始终指向新的字串开头,j一直往后走. int maxLen = 0; bool exist[256] = {false}; for(;j <n ; j++) { if (exist[s[j]]) { maxLen = max(maxLen, j-i); while (s[i] != s[j]) { exist[s[i]] = false; i++; } i++; } else { exist[s[j]] = true; } } return max(maxLen, j-i); } };Java版
public class Solution { public int lengthOfLongestSubstring(String s) { boolean[] exist = new boolean[256]; int i = 0, j = 0, maxLen = 0; for (; j < s.length(); j++) { while (exist[s.charAt(j)]) { exist[s.charAt(i)] = false; i++; } exist[s.charAt(j)] = true; maxLen = Math.max(j - i + 1, maxLen); } return maxLen; } }
相关文章推荐
- leetcode之Longest Substring Without Repeating Characters
- LeetCode 第三题,Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters
- Leetcode3 Longest Substring Without Repeating Characters
- leetcode-03-Longest Substring Without Repeating Characters-python
- LeetCode: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(穷举)-Medium
- Longest Substring Without Repeating Characters--LeetCode
- leetcode3 Longest Substring Without Repeating Characters
- [Leetcode] Longest Substring Without Repeating Characters
- [LeetCode]题解(python):003-Longest Substring Without Repeating Characters
- leetcode刷题:Longest Substring Without Repeating Characters
- LeetCode3——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题解--3-Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters