Leetcode: Longest Substring Without Repeating Characters
2013-12-16 08:30
274 查看
Longest Substring Without Repeating Characters
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.
Idea: The idea of this problem is to check the longest substring (no repeating) for each position, keep track the max length in this process. We need a int character to store the visited information of each character.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s.size() <= 1) return s.size();
int max_len = 0;
int i=0;
while (i<s.size()) {
int j=i;
vector<int> visited(256, 0);
while (j < s.size()) {
if(visited[s[j]] == 0){
visited[s[j]] = 1;
++j;
}
else
{
break;
}
}
max_len = max(max_len, j-i);
while(s[i] != s[j]) {
++i;
}
++i;
}
return max_len;
}
};
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.
Idea: The idea of this problem is to check the longest substring (no repeating) for each position, keep track the max length in this process. We need a int character to store the visited information of each character.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s.size() <= 1) return s.size();
int max_len = 0;
int i=0;
while (i<s.size()) {
int j=i;
vector<int> visited(256, 0);
while (j < s.size()) {
if(visited[s[j]] == 0){
visited[s[j]] = 1;
++j;
}
else
{
break;
}
}
max_len = max(max_len, j-i);
while(s[i] != s[j]) {
++i;
}
++i;
}
return max_len;
}
};
相关文章推荐
- leetcode 3 Longest Substring Without Repeating Characters
- [Leetcode 3, Medium] 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
- LeetCode #Longest Substring Without Repeating Characters#
- LeetCode-Algorithms #003 Longest Substring Without Repeating Characters, Database #177 Nth Highest Salary
- leetcode刷题笔记-Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters -- LeetCode
- LeetCode 003 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
- 【LeetCode-面试算法经典-Java实现】【003-Longest Substring Without Repeating Characters(最长非重复子字符串)】
- [leetcode][3]Longest Substring Without Repeating Characters
- LeetCode Longest Substring Without Repeating Characters
- leetcode Longest Substring Without Repeating Characters