面试题48:最长不含重复字符的子字符串
题目
请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。
LeetCode版本
动态规划
class Solution{
public:
int lengthOfLongestSubstring(string s) {
if(s.empty()) return 0;
int len = s.size();
vector<int> pos(128,-1);
vector<int> dp(len,0);
dp[0] = 1;
pos[s[0]] = 0;
int res = 1;
for(int i = 1; i < len; i++){
int prepos = pos[s[i]];
if(prepos == -1 || i-prepos > dp[i-1]){
dp[i] = dp[i-1] + 1;
res =max(dp[i],res);
}
else{
dp[i] = i- prepos;
res = max(dp[i],res);
}
pos[s[i]] = i;
}
return res;
}
};
滑动窗口(推荐)
class Solution{
public:
int lengthOfLongestSubstring(string s) {
int res =0, len = s.size();
string str = "";
for(int i = 0 ; i < len; i++){
size_t idx = str.find(s[i]);
if(idx != string::npos){
if(res < str.size()) res = str.size();
str.erase(0,idx+1);
}
str += s[i];
}
if(res<str.size()) res = str.size();
return res;
}
};