forked from pezy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
109. Longest Substring Without Repeating Characters/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
设 substr 的起点为 start(s), 终点为 last(l). 每一次迭代,记录一张索引表。 | ||
|
||
abcabcbb | ||
^ ^ | ||
s l | ||
|
||
|char|pos| | ||
|:--:|:-:| | ||
| a | 0 | | ||
| b | 1 | | ||
| c | 2 | | ||
|
||
上图所示,last 指向 `a`, 查询当前表可知,`a` 的位置记录在案,且 `pos >= start`. 故此刻诞生一个 substr. 长度为 `last - start`. s 更新位置为 `pos + 1`. | ||
|
||
有: | ||
|
||
```cpp | ||
auto found = cache.find(s[last]); | ||
if (found != cache.end() && found->second >= start) { | ||
ret = max(ret, last - start); | ||
start = found->second + 1; | ||
} | ||
cache[s[last]] = last; | ||
``` | ||
|
||
注意最终还需要比较一次,返回 `max(ret, s.size() - start)` | ||
|
||
|
||
|
||
|
||
|