力扣第3题 无重复字符的最长子串

力扣第3题 无重复字符的最长子串

class Solution {
    public:
    int lengthOfLongestSubstring(string s) {
        int len = s.size();
	    map<char, int> myMap;
	    map<char, int>::iterator itor;
        int start = 0;
        int maxNum = 0;
        for (int i = 0; i < len; i++)
        {
            itor = myMap.find(s[i]);
            if (itor != myMap.end())
            {
                start = max(itor->second, start);
            }
            myMap[s[i]] = i + 1;
            maxNum = max(maxNum, i - start + 1);
        }
        return maxNum;
    }
};

posted on 2020-02-28 00:07  woodjay  阅读(102)  评论(0编辑  收藏  举报

导航