滑动窗口+哈希表
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int res = 1;
unordered_map<char, int> hashTable;
if(s.length() == 0) return 0;
hashTable[s[0]] = 0;
int start = 0;
for(int i = 1; i < s.length(); i++){
auto item = hashTable.find(s[i]);
if(item != hashTable.end() && item->second >= start){
start = item->second + 1;
}
hashTable[s[i]] = i;
res = max(i - start + 1, res);
}
return res;
}
};
法二: