Longest Substring Without Repeating Characters
来源:互联网 发布:https域名劫持原理 编辑:程序博客网 时间:2024/06/06 03:26
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int len=s.size();
if(s.size()==0) return 0;
int max1=1;
int count;
int j=0;
unordered_map<char,int>imap;
//imap.insert(make_pair(s[0],0));
for(int i=0;i<len-1;i++)
{
for( j=i;j<len;j++)
{
if(imap.find(s[j])==imap.end())
imap.insert(make_pair(s[j],j));
else {
imap.find(s[j])->second=j;
break;
}
}
max1=max(max1,j-i);
imap.clear();
}
return max1;
}
};
这个比较暴力,超时了;
class Solution {
public:int lengthOfLongestSubstring(string s) {
int len=s.size();
if(s.size()==0) return 0;
int max1=1;
int j=0;
unordered_map<char,int>imap;
int start=0,count=0;
//imap.insert(make_pair(s[0],0));
for(int i=0;i<len;i++)
{
if(imap.find(s[i])==imap.end()||imap.find(s[i])->second<start){
// imap.insert(make_pair(s[i],i)); //如果之前有则无法插入;
imap[s[i]] = i;
count++;
}
else {
start=imap.find(s[i])->second+1; //新起点;
imap.find(s[i])->second=i; //指向最后一个(相同的);
// imap[s[i]] = i;
count=i-start+1;
}
max1=max(max1,count);
}
return max1;
}
};
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- struts2与Spring MVC的区别
- Android 5.0学习之使用Palette
- 牢骚
- CAS4添加验证码
- js正则表达式匹配空格
- Longest Substring Without Repeating Characters
- 链表
- Android BlueTooth通信
- LeetCode_144Binary Tree Preorder Traversal
- Nginx学习之十二-负载均衡-加权轮询策略剖析
- 什么是psr-0,psr-1,psr-2标准
- MongoDB与内存
- Nginx学习之十一-Nginx启动框架处理流程
- 整合:求最短路算法 floyd prim dijkstra Kruskal spfa算法简析