我想到的是用数组位置的值代替字符串的重复遍历
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int a[1000];
int ans = 0;
for (int i = 0; i < s.size(); ++i) {
memset(a, 0, sizeof(a));
int Cnt = 0;
for (int j = i; j < s.size(); ++j) {
if (a[s[j]] == 1) {
break;
}
++Cnt;
if (Cnt > ans)ans = Cnt;
a[s[j]] = 1;
}
}
return ans;
}
};