C++,16ms,easy understand,constant space,two indexes


  • 3
    L
    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            if(s.empty()) return 0;
            int p1 = 0, p2 = 1;
            int maxLen = p2 - p1;
            for(; p2 < s.length(); ++p2) {
                int i = p1;
                for(; i < p2; ++i) {
                    if(s[i] == s[p2]) {
                        p1 = i + 1;
                        break;
                    }
                }
                maxLen = max(maxLen, p2 - p1 + 1);
            }
            return maxLen;
        }
    };

  • 0
    D

    without hashmap or other array, good


Log in to reply
 

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