Longest Substring Without Repeating Characters

题目见:点击打开链接

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        map m;
        int curlen, maxlen, i, pos;
        string substr;
        if (s.size() == 0)
                return 0;
        if (s.size() < 2)
                return 1;
        pos = 0;
        curlen = maxlen = 1;
        for (i = 1, m[s[0]] = 0; i < s.size(); ++i)
        {
                if (m.find(s[i]) == m.end())
                {
                        curlen++;
                }
                else
                {
                        maxlen = max(maxlen, curlen);
                        if (m[s[i]] >= pos) //
                        {
                                pos = m[s[i]];
                                curlen = i - m[s[i]];
                        }
                        else
                        {
                                curlen++;
                        }
                }
                m[s[i]] = i;
        }
        maxlen = max(maxlen, curlen);
        return maxlen;
    }
};



你可能感兴趣的:(算法,字符串)