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

Lines changed: 5 additions & 4 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy