Skip to content

Commit 44c0c6a

Browse files
committed
Merge pull request soulmachine#64 from LiEmail/patch-2
Longest Substring Without Repeating Characters
2 parents 967fdd4 + e5ec64c commit 44c0c6a

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

C++/chapGreedy.tex

+5-4
Original file line numberDiff line numberDiff line change
@@ -282,21 +282,22 @@ \subsubsection{代码}
282282
\begin{Code}
283283
// LeetCode, Longest Substring Without Repeating Characters
284284
// 时间复杂度O(n),空间复杂度O(1)
285+
// 考虑非字母的情况
285286
class Solution {
286287
public:
287288
int lengthOfLongestSubstring(string s) {
288-
const int ASCII_MAX = 26;
289+
const int ASCII_MAX = 255;
289290
int last[ASCII_MAX]; // 记录字符上次出现过的位置
290291
int start = 0; // 记录当前子串的起始位置
291292

292293
fill(last, last + ASCII_MAX, -1); // 0也是有效位置,因此初始化为-1
293294
int max_len = 0;
294295
for (int i = 0; i < s.size(); i++) {
295-
if (last[s[i] - 'a'] >= start) {
296+
if (last[s[i]] >= start) {
296297
max_len = max(i - start, max_len);
297-
start = last[s[i] - 'a'] + 1;
298+
start = last[s[i]] + 1;
298299
}
299-
last[s[i] - 'a'] = i;
300+
last[s[i]] = i;
300301
}
301302
return max((int)s.size() - start, max_len); // 别忘了最后一次,例如"abcd"
302303
}

0 commit comments

Comments
 (0)