子串必须是连续的。感觉灵神的 bilibili 视频也可以跟上。感觉和自己竞争的同学好厉害呀。我要是能考 360 我根本不用怕。
cpp
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char, int> last_pos;
int left = 0;
int max_len = 0;
for ( int right = 0; right < s.size(); right++ ) {
char ch = s[right];
if ( last_pos.find( ch ) != last_pos.end() && last_pos[ch] >= left ) {
left = last_pos[ch] + 1;
}
last_pos[ch] = right;
max_len = max( max_len, right - left + 1 );
}
return max_len;
}
};