You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classSolution {
public:intlengthOfLongestSubstring(string s) {
unordered_map<char, int> mp;
int left = 0, n = s.size(), res = 0;
for (int right = 0; right < n; right++) {
mp[s[right]]++;
if (mp[s[right]] > 1) {
res = max(res, right-left);
int i = left;
for (; i < right; i++) {
mp[s[i]]--;
if (s[i] == s[right]) {
break;
}
}
left = i+1;
}
}
res = max(res, n-left);
return res;
}
};
The text was updated successfully, but these errors were encountered:
请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。
示例 1:
示例 2:
示例 3:
提示:
连续子串,最先想到滑动窗口,难点在于左边界移动到哪里?左边界应该移动到从当前左边界开始出现重复的字符后面。举个例子:
代码如下:
The text was updated successfully, but these errors were encountered: