Narcissus's blog Stay Follish, Stay Hungry

3. Longest Substring Without Repeating Characters

2018-05-08
Narcissus

3. Longest Substring Without Repeating Characters

思路暴力求解

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        
        int len=s.length(),max_len=0,cur_len=0;
        map<char,bool>m;
        
        for(int i=0;i<len;i++)
        {
            if(cur_len>max_len)max_len=cur_len;
            m.clear();
            cur_len=0;
            for(int j=i;j<len;j++)
            {
                if(m.find(s[j])==m.end())
                {
                    m[s[j]]=true;
                    cur_len++;
                }
                else
                {
                    break;
                }
            }
        }
        if(cur_len>max_len)max_len=cur_len;
        return max_len;
    }
};

上一篇 2. Add Two Numbers

下一篇 15.3sum

Comments

Content