8000 Create README.md · tomzhang/LeetCode-3@79b2fd0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 79b2fd0

Browse files
committed
Create README.md
1 parent c80bca1 commit 79b2fd0

File tree

1 file changed

+31
-0
lines changed
  • 109. Longest Substring Without Repeating Characters

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
设 substr 的起点为 start(s), 终点为 last(l). 每一次迭代,记录一张索引表。
2+
3+
abcabcbb
4+
^ ^
5+
s l
6+
7+
|char|pos|
8+
|:--:|:-:|
9+
| a | 0 |
10+
| b | 1 |
11+
| c | 2 |
12+
13+
上图所示,last 指向 `a`, 查询当前表可知,`a` 的位置记录在案,且 `pos >= start`. 故此刻诞生一个 substr. 长度为 `last - start`. s 更新位置为 `pos + 1`.
14+
15+
有:
16+
17+
```cpp
18+
auto found = cache.find(s[last]);
19+
if (found != cache.end() && found->second >= start) {
20+
ret = max(ret, last - start);
21+
start = found->second + 1;
22+
}
23+
cache[s[last]] = last;
24+
```
25+
26+
注意最终还需要比较一次,返回 `max(ret, s.size() - start)`
27+
28+
29+
30+
31+

0 commit comments

Comments
 (0)
0