题目
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.
译文
最长不重复子串,给定一个字符串,找出最长的不重复子串。例如,“abcabcbb”的最长不重复子串是“abc”,长度为3。“bbbbb”的最长不重复子串是“b”,长度为1。
分析
medium
我的代码
class Solution {
public:
int lengthOfLongestSubstring(string s) {
char hash[256];
int i, j;
int maxlen=0;
int size = s.size();
for(i=0;i<size;i++){
memset(hash,0,sizeof hash);
for(j = i; j<size; j++){
if(hash[s[j]] == 0){
hash[s[j]] = 1;
}
else{
if(j-i>maxlen){
maxlen = j-i;
}
break;
}
}
if((j==size)&&(j-i>maxlen)){
maxlen = j-i;
}
}
return maxlen;
}
};