My solution with 16-lines in C++ and 20ms used


  • 0
    L
    class Solution {
    public: 
        int lengthOfLongestSubstring(string s) {
        if(s.size()==0)
            return 0;
        int result = 1;
        int tmp = 1;
        for ( int i = 0; i < s.size()-1; i++)
        {
            size_t pos = s.substr( i - tmp + 1, tmp).find(s[i+1]);
            if( pos != string::npos)
            {
                tmp -= pos;
            }else {
                tmp += 1;
            }
            result = (result > tmp) ? result : tmp;
            
        }
        return result;
    }
    

    };


  • 0
    S
        public int method2(String s){
        ArrayList<Character> list = new ArrayList<Character>();
        int maxlen = 0;
        for(int i=0;i<s.length();i++){
            if(list.contains(s.charAt(i))){
                maxlen = Math.max(list.size(),maxlen);
                list.clear();
            }
            list.add(s.charAt(i));
        }
        return maxlen;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.