LeetCode 3 Longest Substring Without Repeating Characters

题目

c++

class Solution {
public:
    map<char,int> m;
    int a[100005];
    int lengthOfLongestSubstring(string s) {
        
        int len = s.length();
        if(len==0)
            return 0;
        a[0]=1;
        m[s[0]]=1;
        int mm=1;
        for(int i=1;i<len;i++)
        {
            if(m[s[i]]==0)
            {
                a[i]=a[i-1]+1;
                mm = max(mm,a[i]);
                m[s[i]]=i+1;
            }
            else
            {
                if(m[s[i]]-1>=i-a[i-1])
                {
                    a[i]=i-m[s[i]]+1;
                   
                }
                else
                    a[i]=a[i-1]+1;
                 mm = max(mm,a[i]);
                 m[s[i]]=i+1;
            }
           
            
        }
        return mm;
    }
};
posted @ 2019-05-29 09:05 Shendu.CC 阅读(...) 评论(...) 编辑 收藏