思路
1.字符串查找重复字符
2.有重复字符则处理重复字符之间的数据并记录长度
代码
int lengthOfLongestSubstring(char* s) {
int maxlen = 0,currlen = 0;
int table[128], i, j, start = 0;
memset(table, 0, sizeof(table));
for (i = 0; s[i] != '\0'; ++i){
if( (++table[s[i]]) == 2 ){//判断重复
if (currlen > maxlen){
maxlen = currlen;//记录最长长度
}
for(j = start; j < i; ++j){ //*start记录重复的字符后一个位置
if (s[j] == s[i]){
table[s[j]] = 1;
start = j+1;
break;
}else{
//清空数组,重置记录长度
--currlen;
table[s[j]] = 0;
}
}
}else{
++currlen;//无重复记录长度自加
}
}
if (currlen > maxlen){
maxlen = currlen;
}
return maxlen;
}