Simple C++ 8-Lines O(N) Solution


  • 0
    M
    class Solution {
    public:
        typedef uint8_t u8;
        int lengthOfLongestSubstring(string s) {
            vector<bool> used(256);
            int l = 0, maxl = 0;
            for (int i = 0; i < s.size(); ++i) {
                while (used[u8(s[i])]) used[u8(s[l++])] = false;
                used[u8(s[i])] = true;
                maxl = max(maxl, i - l + 1);
            }
            return maxl;
        }
    };
    

Log in to reply
 

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