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.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int k = 0;
int maxlen = 0;
map<char, vector<int>>cnt;
vector<char>aa;
while (k-aa.size() < s.length()-maxlen)
{
int kk = k;
while (kk < s.length())
{
cnt[s[kk]].push_back(kk);
if (cnt[s[kk]].size() == 2)
break;
aa.push_back(s[kk]);
kk++;
}
if (aa.size()>maxlen)
maxlen = aa.size();
if (kk == s.length())
break;
int index = find(aa.begin(), aa.end(), s[kk]) - aa.begin();
for (int i = 0; i <= index; i++)
cnt.erase(cnt.find(aa[i]));
aa.erase(aa.begin(), aa.begin() + index + 1);
k = kk;
}
return maxlen;
}
};accepted

本文介绍了一种解决最长无重复字符子串问题的方法。通过使用C++编程语言中的map和vector数据结构来记录字符串中每个字符出现的位置,并实现了一个算法来找出给定字符串中最长的不包含重复字符的子串长度。
722

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



