Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ LeetCode の問題を以下手順で解く

## 解いた問題一覧

| # | Title | Language | Difficulty |
| --- | --------------------------------------------------------------------------------------------------------- | -------- | ---------- |
| 5 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/description/) | Python3 | Medium |
| 143 | [Reorder List](https://leetcode.com/problems/reorder-list/description/) | Python3 | Medium |
| 207 | [Course Schedule](https://leetcode.com/problems/ourse-schedule/description/) | Python3 | Medium |
| 322 | [Coin Change](https://leetcode.com/problems/coin-change/description/) | Python3 | Medium |
| 417 | [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) | Python3 | Medium |
| # | Title | Language | Difficulty |
| --- | ------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ---------- |
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/description/) | Python3 | Medium |
| 5 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/description/) | Python3 | Medium |
| 143 | [Reorder List](https://leetcode.com/problems/reorder-list/description/) | Python3 | Medium |
| 207 | [Course Schedule](https://leetcode.com/problems/ourse-schedule/description/) | Python3 | Medium |
| 322 | [Coin Change](https://leetcode.com/problems/coin-change/description/) | Python3 | Medium |
| 417 | [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) | Python3 | Medium |

## その他

Expand Down
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()
Copy link
Copy Markdown

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などでもいいでしょう。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます。char_to_index のほうがわかりやすそうですね、修正します。

初期化の方法ですが、{} はパッと見で dictset かわかりにくいので、中身が空のときには明示的に dict()set() と書くように意識していました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こんなコメントを頂いた事があります。
TORUS0818/leetcode#13 (comment)

start = -1
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このstartがexclusive(開区間)なのが、途中まで気づきませんでした。コメントなどで追記するか、inclusiveにすると分かりやすいかと思います。

Copy link
Copy Markdown
Owner Author

@thonda28 thonda28 Apr 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます。同じ文字が出てきた場合、過去に出てきたときの index が exclusive になるので合わせて start も exclusive にしていましたが、初見でコードを読む人に(コメントもなしで)それを汲み取らせるのはたしかに酷に思いました。start という変数名にそれ自身が含まれていないことにも違和感がありそう(読み手は inclusive に感じそう)です。

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:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max を and にする手はある気がしますね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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