12ms C++ solution


  • 0
    I
    /* 
        #################
           ^          ^
           start      end
        table t is the lastest position character.
    */
    class Solution {
        public:
            int lengthOfLongestSubstring(string s) {
                int t[256], len, start = 0, end = 0, maxLength = 0;
                memset(t, 0xff, 256*sizeof(int));
                len = s.length();
            
                while(end != len){
                    if(t[s[end]] >= start){
                        if(maxLength < end - start){
                            maxLength = end - start;
                        }
                        start = t[s[end]] + 1;
                    }
                    t[s[end]] = end;
                    ++end;
                }
                if(maxLength < end - start) maxLength = end - start;
                return maxLength;
            }
        };

Log in to reply
 

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