3. Longest Substring Without Repeating Characters
Medium
4883249FavoriteShare
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc"
, with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b"
, with the length of 1.
Example 3:
Input: "pwwkew" Output: 3 Explanation: The answer is"wke"
, with the length of 3. Note that the answer must be a substring,"pwke"
is a subsequence and not a substring.
题意:给你一个字符串,求最长不重复子串的长度。
题解:双指针扫描
class Solution {
public:
int lengthOfLongestSubstring(string s) {
map<char,int> mp;
mp.clear();
int begin = 0;
int end = 0;
int l = 0;
int r = 0;
int Max = 0;
while(end < s.size()){
mp[s[end]]++;
if(mp[s[end]] > 1){
while(begin <= end && mp[s[end]] > 1){
mp[s[begin]]--;
begin++;
}
}
if((end - begin + 1) > Max){
Max = (end - begin + 1);
l = begin;
r = end;
}
end++;
}
return Max;
}
};