1 class Solution { 2 public: 3 int lengthOfLongestSubstring(string s) { 4 const int ASCII_MAX = 256; 5 int last[ASCII_MAX]; 6 memset(last, -1, sizeof(last)); 7 int startPos = 0; int len = 0; 8 for (int i = 0; i < s.size(); ++i) { 9 if (last[s[i]] >= startPos) { 10 len = max(len, i - startPos); 11 startPos = last[s[i]] + 1; 12 } 13 last[s[i]] = i; 14 } 15 return max(len, static_cast<int>(s.size()) - startPos); 16 } 17 };
时间: 2024-10-10 02:53:37