标签:pre char osi padding 算子 tom turn blog cte
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int start = 0, maxlen = 0, position[128] = {-1};
fill(position, position + 128, -1);
for( int i = 0; i < s.size(); ++i){
if( position[s[i]] >= start){//说明在start后面出现了两个重复字母,需要计算子串
maxlen = max( maxlen, i - start);
start = position[s[i]] + 1;
}
position[s[i]] = i;
}
return max(maxlen, (int)s.size() - start);//不要忘记最后字符串结尾的计算
}
};
3. Longest Substring Without Repeating Characters
标签:pre char osi padding 算子 tom turn blog cte
原文地址:http://www.cnblogs.com/zhxshseu/p/798c395acd59e9e6d7482e6566a6c7fa.html