-
Notifications
You must be signed in to change notification settings - Fork 0
3. Longest Substring Without Repeating Characters #6
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # | ||
| # @lc app=leetcode id=3 lang=python3 | ||
| # | ||
| # [3] Longest Substring Without Repeating Characters | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| index_map = dict() | ||
| start = -1 | ||
| length = 0 | ||
| for i, char in enumerate(s): | ||
| if char in index_map: | ||
| start = max(start, index_map[char]) | ||
| index_map[char] = i | ||
| length = max(length, i - start) | ||
| return length | ||
| # @lc code=end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # | ||
| # @lc app=leetcode id=3 lang=python3 | ||
| # | ||
| # [3] Longest Substring Without Repeating Characters | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| char_index_map = dict() | ||
| start = -1 | ||
|
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. このstartがexclusive(開区間)なのが、途中まで気づきませんでした。コメントなどで追記するか、inclusiveにすると分かりやすいかと思います。
Owner
Author
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. コメントありがとうございます。同じ文字が出てきた場合、過去に出てきたときの index が exclusive になるので合わせて 7c7303d で inclusive 版も提出してみました。 |
||
| longest_length = 0 | ||
| for i, char in enumerate(s): | ||
| if char in char_index_map: | ||
| start = max(start, char_index_map[char]) | ||
| char_index_map[char] = i | ||
| longest_length = max(longest_length, i - start) | ||
| return longest_length | ||
| # @lc code=end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # | ||
| # @lc app=leetcode id=3 lang=python3 | ||
| # | ||
| # [3] Longest Substring Without Repeating Characters | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
| def lengthOfLongestSubstring(self, s: str) -> int: | ||
| char_to_index = dict() | ||
| start = 0 | ||
| longest_length = 0 | ||
| for i, char in enumerate(s): | ||
| if char in char_to_index: | ||
|
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 を and にする手はある気がしますね。 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. (すみません、同じコメントをしてしまいました。) |
||
| start = max(start, char_to_index[char] + 1) | ||
| char_to_index[char] = i | ||
| longest_length = max(longest_length, i - start + 1) | ||
| return longest_length | ||
| # @lc code=end | ||
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.
char_index_map = {}
名前は、char_to_indexなどでもいいでしょう。
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.
コメントありがとうございます。
char_to_indexのほうがわかりやすそうですね、修正します。初期化の方法ですが、
{}はパッと見でdictかsetかわかりにくいので、中身が空のときには明示的にdict()や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.
こんなコメントを頂いた事があります。
TORUS0818/leetcode#13 (comment)