首页 > 代码库 > problem 3. Longest Substring Without Repeating Characters

problem 3. Longest Substring Without Repeating Characters

 1 class Solution { 2 public: 3     int lengthOfLongestSubstring(string s) { 4         vector<int> signs(256, -1); 5         int max_length = 0; 6         int last_valid = -1; 7         for (size_t i = 0; i < s.size(); ++i){ 8             char c = s[i]; 9             int last_occ = signs[c];10             if (last_occ < last_valid){11                 last_occ = last_valid;12             }13             if (last_occ > last_valid){14                 last_valid = last_occ;15             }16             int length = i - last_occ;17             if (length > max_length){18                   max_length = length;19             }20             21             signs[c] = i;22         }23         if (max_length == 0){24             max_length = s.size();25         }26         return max_length;27     }28 };

 

problem 3. Longest Substring Without Repeating Characters