3.Longest Substring Without Repeating Characters
来源:互联网 发布:端口查询 编辑:程序博客网 时间:2024/05/16 09:24
Longest Substring Without Repeating Characters
Total Accepted: 49054 Total Submissions: 226266My SubmissionsGiven 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.
思路:首尾指针后移,使用中间存储局部最大值,使用一个标记辅助数组(重点在于其更新过程)。时间复杂度O(n)
代码:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s.size()==0) return 0;
bool exist[256];
//初始化
for(int i=0;i<256;++i)
exist[i]=false;
int start=0,end=0,char_count=0;
while(end<s.size()){
//遇到相同的字符,则更新结果,当前子字符串,和辅助标记数组
if(exist[s[end]]){
char_count=max(char_count,end-start);
while(s[start]!=s[end]){
exist[s[start]]=false;
++start;
}
++start;
++end;
}else{//否则,继续
exist[s[end]]=true;
++end;
}
}
return max(char_count,end-start);
}
};
- [LeetCode]3.Longest Substring Without Repeating Characters
- LeetCode 3.Longest Substring Without Repeating Characters
- LeetCode --- 3. Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- [Leetcode] 3. Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters
- [leetcode] 3.Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- [leetcode] 3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters
- [Leetcode]3. Longest Substring Without Repeating Characters
- leetcode 3. Longest Substring Without Repeating Characters
- 【Leetcode】3. Longest Substring Without Repeating Characters
- Zookeeper:服务器和会话
- 2.Add Two Numbers
- java 解惑3 string 字符串
- java读取配置文件
- 从今天起,写一本关于LLVM的书----《深入理解LLVM》
- 3.Longest Substring Without Repeating Characters
- HMC5883L 电子指南针用树莓派进行磁场干扰过滤 校准
- POJ 3608 Bridge Across Islands(模板小汇)
- STL count_if() with lambda expression
- 1014. Waiting in Line (30)
- 深入C#中get与set的详解
- C# delegate event
- iOS系统网络抓包方法
- style、currentStyle、getComputedStyle区别介绍