My 25ms c++ solution


  • 0
    N
    int lengthOfLongestSubstring(string s) {                                                                                                                      
      vector<int> sMap(256, 0);                                                                                                                                             
      int max = 0, begin = 0, end = 0;                                                                                                                                      
      for (; end < s.size(); end++) {                                                                                                                                       
        if (!sMap[s[end]]) {                                                                                                                                                
          sMap[s[end]] = 1;                                                                                                                                                 
        }                                                                                                                                                                   
        else {                                                                                                                                                              
          max = max >= (end - begin) ? max : (end - begin);                                                                                                                 
          while (s[begin] != s[end]) {                                                                                                                                      
            sMap[s[begin++]] = 0;                                                                                                                                           
          }                                                                                                                                                                 
          begin++;                                                                                                                                                          
        }                                                                                                                                                                   
      }                                                                                                                                                                     
      return max = max >= (end - begin) ? max : (end - begin);                                                                                                              
    }

Log in to reply
 

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