3. 无重复字符的最长子串

class Solution {
public:
    int lengthOfLongestSubstring(string s) {

        int res=0,rul=1;
        map <char, int> word;
        for(int i=1;i<=s.length();++i){
            if(word[s[i-1]]>=rul){
                rul=word[s[i-1]]+1;
                word[s[i-1]]=i; 
            }else{
                word[s[i-1]]=i;
                res = max(res,i-rul+1);
            }
        }
        return res;
    }
};

 

posted @ 2020-05-31 15:27  ikefire  阅读(101)  评论(0编辑  收藏  举报