C++ 12ms solution


  • 0
    H
    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            int bitmap[256];
            memset(bitmap, -1, sizeof(bitmap));
     
            int best = 0, start = 0;
            for (int i=0, len=s.length(); i < len; ++i) {
                int iLast = bitmap[s[i]];
                if (iLast != -1 && iLast >= start) { // include iLast
                    int temp = i - start; // (i - 1) - start + 1; // get rid of 'this i'
                    if (temp > best) best = temp;
                    start = iLast + 1; // calc new start
                }
                bitmap[s[i]] = i; // overwrite
            }
            
            int temp = s.length() - start;
            if (temp > best) best = temp;
            
            return best;
        }
    };
    

  • 0
    S
    This post is deleted!

Log in to reply
 

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