File tree 1 file changed +5
-4
lines changed
1 file changed +5
-4
lines changed Original file line number Diff line number Diff line change @@ -282,21 +282,22 @@ \subsubsection{代码}
282
282
\begin {Code }
283
283
// LeetCode, Longest Substring Without Repeating Characters
284
284
// 时间复杂度O(n),空间复杂度O(1)
285
+ // 考虑非字母的情况
285
286
class Solution {
286
287
public:
287
288
int lengthOfLongestSubstring(string s) {
288
- const int ASCII_MAX = 26 ;
289
+ const int ASCII_MAX = 255 ;
289
290
int last[ASCII_MAX]; // 记录字符上次出现过的位置
290
291
int start = 0; // 记录当前子串的起始位置
291
292
292
293
fill(last, last + ASCII_MAX, -1); // 0也是有效位置,因此初始化为-1
293
294
int max_len = 0;
294
295
for (int i = 0; i < s.size(); i++) {
295
- if (last[s[i] - 'a' ] >= start) {
296
+ if (last[s[i]] >= start) {
296
297
max_len = max(i - start, max_len);
297
- start = last[s[i] - 'a' ] + 1;
298
+ start = last[s[i]] + 1;
298
299
}
299
- last[s[i] - 'a' ] = i;
300
+ last[s[i]] = i;
300
301
}
301
302
return max((int)s.size() - start, max_len); // 别忘了最后一次,例如"abcd"
302
303
}
You can’t perform that action at this time.
0 commit comments