当前位置: 代码迷 >> 综合 >> Leetcode 3. Longest Substring Without Repeating Characters 滑动窗口经典
  详细解决方案

Leetcode 3. Longest Substring Without Repeating Characters 滑动窗口经典

热度:10   发布时间:2024-02-12 17:20:23.0
  • r右移,一旦出现重复,l右移,在不重复的情况下,更新res
class Solution {
public:int lengthOfLongestSubstring(string s) {unordered_map<char, int> window;int res=0;int l=0, r=0;while(r<s.size()){char c1=s[r];r++;window[c1]++;while(window[c1]>1){char c2=s[l];window[c2]--;l++;}res = max(res, r-l);}return res;}
};
  相关解决方案