原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/
链接:http://blog.youkuaiyun.com/linhuanmars/article/details/19949159 给出了比较详细的分析,同时也给出了一个需要一hashset的线性是时间方法。
这道题目,我做的时候,采用的同样是窗口的思想,利用一个浮动的窗口,来判断是否有重复的字符。 如何判断是否有重复的字符,用一个260的char数组来表示当前窗口里面所有出现的字符的数量。然后判断start-end这一个区间里面,是否出现了重复字符,如果有重复字符,那么调整start的位置,直到消除重复字符,如此反复,就能够知道最长的区间是多少了。
源代码如下:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int text[256] = {0};
int isz = s.length();
int mmax = 0;
int count = 0;
int start = 0 ;
int end = 0;
while (end < isz)
{
text[s[end]] ++;
count ++;
if(text[s[end]] >1 )
{
//cout << count << endl;
mmax = mmax > count-1? mmax : count-1;
while (text[s[end]] > 1)
{
text[s[start]] --;
start ++;
count --;
}
//cout <<start << " " << count <<endl;
}
end ++;
}
mmax = mmax > count? mmax : count;
return mmax;
}
};