您的位置:首页 > 其它

Longest Substring Without Repeating Characters

2015-11-19 13:01 369 查看
题意:

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) {
const int SIZE=256;
int record[SIZE];
memset(record,-1,sizeof(record));// 当顺序遍历到字符 s[i],s[i] 最后出现的位置

int final_ans=0;
int new_start=0; // 这一轮不重复字符串的起点
for(int i=0;i<s.size();++i){
if(record[(int)s[i]]>=new_start)
new_start=record[(int) s[i]]+1;//新的起点
if(i-new_start+1>final_ans) // 更新 final_ans
final_ans=i-new_start+1;
record[(int)s[i]]=i; // 更新 s[i] 最后一次出现的地方
}
return final_ans;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  字符串处理