LeetCode 3 Longest Substring Without Repeating Characters

Posted dacc123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }
};

以上是关于LeetCode 3 Longest Substring Without Repeating Characters的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 3. Longest Substring Without Repeating

3. 没有重复字母的最长子串 [leetcode 3: Longest Substring Without Repeating Characters]

3. 没有重复字母的最长子串 [leetcode 3: Longest Substring Without Repeating Characters]

leetcode longest consecutive sequence

leetcode 3. Longest Substring Without Repeating Characters (Python版)

LeetCode: Longest Consecutive Sequence [128]