class Solution {
public:
int lengthOfLongestSubstring(string s) {
int length = s.size();
int max = 0, cur=0;
char single;
map<char, int> countChar;
for(int j=0;j<length;j++){
cur=j;
for(int i=j; i<length; i++){
single = s[i];
cur++;
if(!countChar[single] && i != length-1)
countChar[single]++;
else if(!countChar[single] && i == length-1){
if((cur-j)>max)
max = cur-j;
countChar.clear();
//cout<< 1 << endl;
break;
}
else if(countChar[single]){
if((cur-j-1)>max)
max = cur-j-1;
countChar.clear();
//cout << 1 <<endl;
break;
}
}
}
return max;
}
};
应该考虑只遍历一次的方法。