-
Notifications
You must be signed in to change notification settings - Fork 0
word ladder #11
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: master
Are you sure you want to change the base?
word ladder #11
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,80 @@ | ||
| 1回目。答えを見た。 | ||
| 変数名がキャメルケースなのは違和感があるが、入力がキャメルケースなので少し納得した。 | ||
|
|
||
| ```py | ||
| from collections import deque | ||
|
|
||
| class Solution: | ||
| def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: | ||
| wordSet = set(wordList) | ||
| if endWord not in wordSet: | ||
| return 0 | ||
|
|
||
| queue = deque([(beginWord, 1)]) | ||
| while queue: | ||
| word, steps = queue.popleft() | ||
| if word == endWord: | ||
| return steps | ||
|
|
||
| for i in range(len(word)): | ||
| for ch in range(26): | ||
|
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. 個人的にはこの解答だと入力の対象になる文字列が英語小文字以外から増えた時に困るなあと思いました
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. 確かにそうですね。こちらで指摘されているように、string.ascii_lowercase を使うとよいかもしれないと思いました。 |
||
| transformed = word[:i] + chr(ord('a') + ch) + word[i+1:] | ||
| if transformed in wordSet: | ||
|
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. ネストが深すぎるため、ぱっと見て読むのに負荷を感じました。
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. なるほど、ご意見ありがとうございます!! |
||
| wordSet.remove(transformed) | ||
| queue.append((transformed, steps + 1)) | ||
| return 0 | ||
| ``` | ||
|
|
||
| 2回目。変数をリネームした(入力はそのままキャメルケースだが、新たに宣言する変数はスネークケースにした。混在するのは欠点だが、入力とローカル変数の区別がしやすい利点もある)。 | ||
|
|
||
| ```py | ||
| from collections import deque | ||
|
|
||
| class Solution: | ||
| def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: | ||
| word_set = set(wordList) | ||
| if endWord not in word_set: | ||
| return 0 | ||
|
|
||
| queue = deque([(beginWord, 1)]) | ||
|
|
||
| while queue: | ||
| word, num_steps = queue.popleft() | ||
|
|
||
| if word == endWord: | ||
| return num_steps | ||
|
|
||
| for i in range(len(word)): | ||
| for alpha_offset in range(26): | ||
| transformed_word = word[:i] + chr(ord('a') + alpha_offset) + word[i + 1:] | ||
| if transformed_word in word_set: | ||
| word_set.remove(transformed_word) | ||
| queue.append((transformed_word, num_steps + 1)) | ||
| return 0 | ||
| ``` | ||
|
|
||
| 3回目。 | ||
|
|
||
| ```py | ||
| from collections import deque | ||
|
|
||
| class Solution: | ||
| def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: | ||
| word_set = set(wordList) | ||
| if endWord not in word_set: | ||
| return 0 | ||
|
|
||
| queue = deque([(beginWord, 1)]) | ||
|
|
||
| while queue: | ||
| word, num_steps = queue.popleft() | ||
| if word == endWord: | ||
| return num_steps | ||
| for i in range(len(word)): | ||
| for alpha_offset in range(26): | ||
|
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. マジックナンバーを使っているので、自分ならstring.ascii_lowercaseを使うか、26は別で定義します。 |
||
| transformed_word = word[:i] + chr(ord('a') + alpha_offset) + word[i + 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.
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. とても参考になります、ありがとうございます! 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. 文字列の連結には、f-stringもしくはjoinを使うと良いと思います。
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. これは知りませんでした。ありがとうございます! |
||
| if transformed_word in word_set: | ||
| word_set.remove(transformed_word) | ||
| queue.append((transformed_word, num_steps + 1)) | ||
| return 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.
leetcodeならば、関数名はともかく、引数となっている変数名に関しては変更可能です。
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.
Python の標準的な naming convention では、snake 使いますね。
https://peps.python.org/pep-0008/#naming-conventions
Python は呼び出し方が、位置とキーワードの2つがあるので、本当は変更すると動かなくなる可能性はあります。
https://docs.python.org/3/tutorial/controlflow.html#special-parameters