7-line simple c++ solution.How could it be even better?


  • 0
    B
    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
          int index[257] = {0};
            for (int i = 0,j = 0; i < s.length();i ++) {
                j = max(j, index[s[i]]);
                index[s[i]] = i + 1;
                index[256] = max(index[256],i - j + 1);
            }
            return index[256];
        }
    };
    

Log in to reply
 

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