美文网首页Leetcode
Leetcode #3 无重复字符的最长子串

Leetcode #3 无重复字符的最长子串

作者: HU兔兔 | 来源:发表于2020-02-01 15:24 被阅读0次
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int len=0;
        int i=0;
        int max=0;
        int m[128];
        for(i=0;i<128;i++){
            m[i]=-1;
        }
        i=0;
        while(i<s.size()){
            if(m[s[i]]==-1){
                len+=1;
            }
            else{
                if(len>=i-m[s[i]]&&i!=m[s[i]]){
                    len=i-m[s[i]];
                }
                else{
                    len++;
                }
            }
            m[s[i]]=i;
            i++;
            max=max>=len?max:len;
        }
        return max;
    }
};

相关文章

网友评论

    本文标题:Leetcode #3 无重复字符的最长子串

    本文链接:https://www.haomeiwen.com/subject/qkhxxhtx.html