Longest Substring Without Repeating Characters

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(s=="")
        return 0;
        map<char,int> key;
        for(char i = 'a';i<='z';i++)
        key[i] = -1;
        int count = s.length();
        int i = 0;
        int result = 0;
        int temp = 0;
        int pre = 0;
        while(i<count)
        {
            if(key[s[i]]<pre)
            {
                temp++;
                key[s[i]] = i;
            }
            else
            {
                if(temp>result)
                {
                    result = temp;
                }
                temp = i - key[s[i]];
                pre = key[s[i]];
                key[s[i]] = i;
            }
            i++;
        }
        if(temp>result)
        result = temp;
        return result;
    }
};
原文地址:https://www.cnblogs.com/727713-chuan/p/3337615.html