File tree 1 file changed +8
-8
lines changed
1 file changed +8
-8
lines changed Original file line number Diff line number Diff line change @@ -287,18 +287,18 @@ \subsubsection{代码}
287
287
int lengthOfLongestSubstring(string s) {
288
288
const int ASCII_MAX = 26;
289
289
int last[ASCII_MAX]; // 记录字符上次出现过的位置
290
+ int start = 0; // 记录当前子串的起始位置
291
+
290
292
fill(last, last + ASCII_MAX, -1); // 0也是有效位置,因此初始化为-1
291
- int len = 0, max_len = 0;
292
- for (size_t i = 0; i < s.size(); i++, len++) {
293
- if (last[s[i] - 'a' ] >= 0) {
294
- max_len = max(len, max_len);
295
- len = 0;
296
- i = last[s[i] - 'a' ] + 1;
297
- fill(last, last + ASCII_MAX, -1); // 重新开始
293
+ int max_len = 0;
294
+ for (int i = 0; i < s.size(); i++) {
295
+ if (last[s[i] - 'a' ] >= start) {
296
+ max_len = max(i - start, max_len);
297
+ start = last[s[i] - 'a' ] + 1;
298
298
}
299
299
last[s[i] - 'a' ] = i;
300
300
}
301
- return max(len , max_len); // 别忘了最后一次,例如"abcd"
301
+ return max((int)s.size() - start , max_len); // 别忘了最后一次,例如"abcd"
302
302
}
303
303
};
304
304
\end {Code }
You can’t perform that action at this time.
0 commit comments