class Solution {
public:
//哈希表 + 双指针
int lengthOfLongestSubstring(string s) {
unordered_map<int, int> dic;
int res = 0;
for(int i = 0, j = 0; j < s.length(); j++) {
dic[s[j]]++;
while(dic[s[j]] > 1) {
dic[s[i]]--;
i++;
}
res = max(res, j - i + 1);
}
return res;
}
};
剑指 Offer 48. 最长不含重复字符的子字符串
最新推荐文章于 2021-12-06 17:37:23 发布