Q3. Longest Substring Without Repeating Characters
分析
C++代码
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map <char, int> hash;
int res = 0;
int cur_res = 0;
for (int i = 0; i<s.size(); i++){
if (hash.find(s[i]) == hash.end()){
cur_res += 1;
}else{
cur_res = min((i-hash[s[i]]), (cur_res+1));
}
hash[s[i]] = i;
res = res>cur_res?res:cur_res;
}
return res;
}
};Last updated