-
Notifications
You must be signed in to change notification settings - Fork 0
3. Longest Substring Without Repeating Characters #3
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
base: main
Are you sure you want to change the base?
Conversation
| * 実行時間: TLE | ||
|
|
||
| * いつまで経っても `tail` == `len(s)-1` とならないため、TLEとなった | ||
| - 上記原因が分からず、1時間経っていたため答えを見た |
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.
tail -= 1 してますからね。
本当はしたかったことは、
substring = substring[1:]
だとは思うのですが、しかし、それだと s[tail] がまだ中にあるかもしれません。なので頭から探して、そこまで動かさないといけません。
頭の中のシミュレーターの精度を上げましょう。それと、「人が手でやるとしたらどうするか」と「複数人でシフトを組んでやってもらうとしたらどうするか」を考えてみるといいかと思います。
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| left_i = 0 | ||
| max_substring_length = 0 | ||
| seen_char_set = set() |
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.
個人的にはseenだと一度見たものは取り除かれないみたいな風に見えてしまうかもしれないので、containing_char_setとかでもいいと思います。
| substring_length = 0 | ||
| substring = '' | ||
| tail = 0 | ||
| while tail != len(s)-1: |
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.
- の両側にスペースを空けることをお勧めします。
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| left_i = 0 | ||
| max_substring_length = 0 | ||
| char_set = set() |
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.
変数名に型名を入れてもあまり情報は増えないため、入れないほうがよいと思います。
| max_substring_length = 0 | ||
| seen_char_set = set() | ||
|
|
||
| for right_i in range(len(s)): |
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.
(コメントが保留中のまま送れませんでした、失礼しました…)
memoが整理されていて読みやすかったです!
好みの範疇だと思うのですが、right_i自体が必要な個所が少ないので、
for right_i, right_char in enumerate(s):
でもいいかなと思いました。
| ```python | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| left_i = 0 |
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.
以前以下のようなコメントをいただいた事があります。
TORUS0818/leetcode#35 (comment)
| tail = 0 | ||
| while tail != len(s)-1: | ||
| substring_length = max(substring_length, len(substring)) | ||
| substring += s[tail] |
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.
こういう話題もあったので共有します。
nittoco/leetcode#4 (comment)
3. Longest Substring Without Repeating Characters