LeetCode_3 无重复字符的最长子串 longest-substring-without-repeating-characters

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sumword_/article/details/86773678

滑动窗口,简单题


int lengthOfLongestSubstring(string s) {
    unordered_map<char, int> mymap;
    int pre = 0;
    int maxx = 0;
    for(int i = 0; i < s.size(); i++){
        if(mymap.find(s[i]) == mymap.end()){
            mymap[s[i]] = i;
            maxx = max(maxx,i-pre+1);
//            continue;
        }
        else if(mymap[s[i]] < pre){
            mymap[s[i]] = i;
            maxx = max(maxx,i-pre+1);
        }
        else{
            pre = mymap[s[i]]+1;
            mymap[s[i]] = i;
            maxx = max(maxx,i-pre+1);
        }
    }
    return maxx;
}

猜你喜欢

转载自blog.csdn.net/sumword_/article/details/86773678