-
Notifications
You must be signed in to change notification settings - Fork 0
Longest Substring Without Repeating Characters #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Exzrgs
merged 3 commits into
main
from
arai60-longest-substring-without-repeating-characters
May 15, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
arai60/longest-substring-without-repeating-characters/step1.py
This file contains hidden or 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,24 @@ | ||
| """ | ||
| 出現した文字を集合で管理して判定するとよさそう | ||
| 連続でなければならないのに気づいていなかった。サンプルを最初にちゃんと確認しなければ。 | ||
| 連続ならば、すでに出現したものが現れたときにリセットすればよい | ||
| いや、被りが来た時点で、被りの次のindexから始めることにしないといけない | ||
| 辞書で管理するほうが良いか | ||
| if seen.get(s[i]) andというような条件にしてしまっていて、seen.get(s[i]) == 0の場合が弾かれてしまっていたのに気づくのに時間がかかってしまった... | ||
|
|
||
| 経過時間:22m | ||
| """ | ||
|
|
||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| seen = dict() | ||
| answer = 0 | ||
| start_index = 0 | ||
| for i in range(len(s)): | ||
| if s[i] in seen and seen.get(s[i]) >= start_index: | ||
| answer = max(answer, i-start_index) | ||
| start_index = seen.get(s[i])+1 | ||
| seen[s[i]] = i | ||
| answer = max(answer, len(s)-start_index) | ||
|
|
||
| return answer | ||
21 changes: 21 additions & 0 deletions
21
arai60/longest-substring-without-repeating-characters/step2.py
This file contains hidden or 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,21 @@ | ||
| """ | ||
| 算術をスペース入れずにやってしまう癖を直す | ||
| start_index → left | ||
| enumerateを使う | ||
| answerに代入する処理の位置を変える | ||
|
|
||
| char_to_indexのほうがよさそう | ||
| """ | ||
|
|
||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| char_to_index = dict() | ||
| answer = 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. max_lengthの方が分かりやすいかもしれないです。 |
||
| left = 0 | ||
| for right, c in enumerate(s): | ||
| if c in char_to_index and char_to_index[c] >= left: | ||
| left = char_to_index[c] + 1 | ||
| char_to_index[c] = right | ||
| answer = max(answer, right - left + 1) | ||
|
|
||
| return answer | ||
17 changes: 17 additions & 0 deletions
17
arai60/longest-substring-without-repeating-characters/step3.py
This file contains hidden or 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,17 @@ | ||
| """ | ||
| 1回目: 3m55s | ||
|
|
||
| """ | ||
|
|
||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| seen = dict() | ||
| answer = 0 | ||
| left = 0 | ||
| for right, c in enumerate(s): | ||
| if c in seen and seen[c] >= left: | ||
| left = seen[c] + 1 | ||
| seen[c] = right | ||
| answer = max(answer, right - left + 1) | ||
|
|
||
| return answer |
16 changes: 16 additions & 0 deletions
16
arai60/longest-substring-without-repeating-characters/step4.py
This file contains hidden or 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,16 @@ | ||
| """ | ||
| returnの前の改行をなくす | ||
| answer → max_length | ||
| """ | ||
|
|
||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| char_to_index = dict() | ||
| max_length = 0 | ||
| left = 0 | ||
| for right, c in enumerate(s): | ||
| if c in char_to_index and char_to_index[c] >= left: | ||
| left = char_to_index[c] + 1 | ||
| char_to_index[c] = right | ||
| max_length = max(max_length, right - left + 1) | ||
| return max_length |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(単純に忘れただけだと思いますが)+-の両脇にスペースを入れた方が良いと思います。