leetcode 3. Longest Substring Without Repeating Characters
2016-03-29 13:52
513 查看
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
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
相关文章推荐
- QPS PV
- 别再用print来调试啦:logging模块超简明指南
- Awesome Python
- 使用boost库序列化传输对象
- windows系统设置mysql自动定时备份
- HSV颜色空间
- 饿了么移动APP的架构演进(转)
- zabbix安装配置2
- Android中的 mvp 开发模式
- 计算机(Linux系统)启动流程
- ionic <ion-content>标签
- dataguard的启动与关闭
- ajax获取其他网站接口信息
- QML ComboBox 图片加文字
- 《老罗的Android之旅》导读PPT
- Java获得某目录下文件总大小
- springMVC json自动将date类型转换为long
- 分享内容到微博、QQ空间、人人网、开心网等社区
- 大数据下的Distinct Count(一):序
- swift 自定义画渐变色折线图