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;
}
};
本文介绍了一种寻找字符串中最长无重复字符子串的方法,通过遍历并使用辅助数据结构记录字符出现的位置来实现。文章提供了一个C++示例代码,展示了如何有效地更新最大长度。
691

被折叠的 条评论
为什么被折叠?



