My c++ solution using dp ,which beats 97%


  • 4
    R
    class Solution {
        public:
            int lengthOfLongestSubstring(string s) {
                if(!s.size()) return 0;
                int max = 0;
                int count = 0;
                int current = -1;
                for(int i = 0; i < 256; ++i) {
                    tmp[i] = -1;
                }
                for(int i = 0; i < s.length(); ++i) {
                    int step = s[i] - '\0';
                    if(tmp[step] == -1 || tmp[step] <= current)
                    {
                        tmp[step] = i;
                        ++count;
                    }
                    else
                    {
                        if(count > max)
                           max = count;
                        count = i - tmp[step];
                        current = tmp[step];
                        tmp[step] = i;
                    }
                    
                }
                if(count > max)
                        max = count;
                return max;
            }
        private:
            int tmp[256];
    }

  • 0
    Q

    you don't need to do - '\0', because '\0' is 0.


Log in to reply
 

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