intlongestSubstringWithoutDuplication(string s){ int i, j = 0, n =s.size(); unordered_map<char,int> counter; int res = 0; while (i < n) { char c = s[i]; counter[c]++; if (counter[c] == 1) { //新来了一个不重复元素,则窗口扩大了 res = max(res, i - j + 1) } i++; while (j < i && counter[c] > 1) { //如果现在窗口已经不在符合条件,则一直往前移动left,直到窗口重新满足 counter[s[j]]--; j++; } } }