题目描述:
请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。
假设字符串中只包含从’a’到’z’的字符。
样例
输入:"abcabc"
输出:3
分析:
双指针法,第一个指针i从头开始遍历,同时将哈希表的值++,一旦遇见重复的,则从j开始遍历,同时哈希表值--,直到遍历到重复的那个值位置,j为该位置++,重复上述过程。这一方法与数组连续子序列最大和有点像,不过那个是遇见累加和为负数就重新规划起点,本题是遇见重复的就重新规划起点。
class Solution {
public:
int longestSubstringWithoutDuplication(string s) {
unordered_map<char,int> m;
int ans = 0;
int i,j = 0;
for(i = 0;i < s.size();i++){
char t = s[i];
if(!m[t]){
m[t]++;
}
else{
ans = max(ans,i - j);
int p = j;
while(s[p] != t) m[s[p]]--,p++;
j = ++p;
}
}
ans = max(ans,i - j);
return ans;
}
};