class Solution { public: int lengthOfLongestSubstring(string s) { if(!s.length()) return 0; vector<int> index(256,0); int maxLen = 0, n = s.length(); for (int j = 0, i = 0; j < n; j++) { i = max(index[s[j]],i); maxLen = max(maxLen, j - i + 1); index[s[j]] = j + 1; } return maxLen; } };
加1是为了从重复元素的下一个开始计算
愿为天下目,萃聚六路华