problem:3. 无重复字符的最长子串
思路:
Accode:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int ans = 0;
unordered_map<int,int> cnt;
deque<int> deq;
int len = s.length();
for(int i=0;i<len;i++){
while(deq.size() && cnt[s[i]]>=1){
cnt[s[deq.front()]]--;
if(cnt[s[deq.front()]]==0) cnt.erase(s[deq.front()]);
deq.pop_front();
}
deq.push_back(i);
cnt[s[i]]++;
ans = max(ans,(int)cnt.size());
}
return ans;
}
};
时间复杂度:$o(n)$