要求:rt
思路:动态规划不够直观,直接哈希加滑动窗口
class Solution {
public:
int lengthOfLongestSubstring(string s) {
map<int,int> m;
int maxlen=0;
for(int start=0,end=0;end<s.length();++end){
if(m.count(s[end])&&m[s[end]]>=start)
start=m[s[end]]+1;
m[s[end]]=end;
maxlen=max(maxlen,end-start+1);
}
return maxlen;
}
};