Conversation
liquo-rice
reviewed
Apr 12, 2026
| visited.add(beginWord) | ||
|
|
||
| def is_diff_one(word1, word2): | ||
| if not len(word1) == len(word2): |
|
|
||
| ## 他の方のコードを読んだ所感 | ||
| https://github.com/hemispherium/LeetCode_Arai60/pull/19/changes/efa4e350aa9dd9220904e7b708ad2fac2c560209 | ||
| step1にてqueueから出したwordをtempと名づけていてぱっと見これが何を意味しているのかわかりづらくなる原因となると思ったのでcurrentWordとかにした方が良いと感じた。この方は最初にendWordがwordListに存在するか判定しているが、私の方では確認していないのでif文の中にif文を書くネスト構造となってしまった。wordSetから訪問済みの値を消すことでvisitedと同等の働きをしているが、個人的には可読性が低くなってしまうのではと感じた。 |
There was a problem hiding this comment.
unvisited_wordsとかにしたら、可読性も担保されそうです。
| while q: | ||
| currentWord, distance = q.popleft() | ||
| for i in range(len(currentWord)): | ||
| for c in "abcdefghijklmnopqrstuvwxyz": |
| @@ -0,0 +1,100 @@ | |||
|
|
|||
| ## step1: | |||
| これはbeginWordから文字の距離が小さい順から広さ優先探索した方がいいと思い、queueを使った。beginWordから始まり、すでに訪れていない&文字の距離が1であるものをqueueにプッシュしていき、もしendWordに一致するものが来たらそこが答えの距離である。最初queueでpop, appendしてれば勝手にキューの動作をすると思っていたがエラーが出てpopleftを使わなければならないことを知った。 | |||
| ``` | ||
|
|
||
| ## step2: | ||
| leetcode上で実行速度が速い人のコードを見てみると"abcde..."各アルファベットをqueueでpopした文字のi番目にそれぞれ代入して、、wordList->set(wordList)にそれぞれが入っているかで判別していた。こうすると元々自分のコードでO(N*L, Nはlen(wordList), Lはwordの長さ)だったのが、O(26*L)とNのサイズが大きい場合に非常に有効な方法となっていた。なので一旦それで実装してみた。けど初見で解く場合は、step1でのコードを書いて面接官からもう少し早くできるか言われた場合にstep2のコードを書くのが自然ではないかと感じる。 |
Owner
Author
There was a problem hiding this comment.
ここはwhile q:内の計算量ですね。アルゴリズム全体の計算量はどう見積もるのかわからなかったのでスルーしてしまいました...
最悪のケースで全てのwordを巡ってから答えがもとまるので最悪計算量がO(N26L)ですかね。
nodchip
reviewed
Apr 15, 2026
| from collections import deque | ||
| class Solution: | ||
| def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: | ||
| q = deque() |
There was a problem hiding this comment.
q はキューを表しているのでしょうか。変数名に型名を入れても、読み手にとってあまり有益な情報とならないと思います。中に入っている値がどのようなものかを変数名として付けたほうが良いと思います。また、自分の場合は、複数値が含まれる場合は、複数形の s で終えることが多いです。 word_and_distances はいかがでしょうか?
なお、 dict の場合は (キー)_to_(値) と命名するのをよく見かけます。
Owner
Author
There was a problem hiding this comment.
とても有益な情報をありがとうございます!確かに出てくる情報を命名に込めた方が直感的にわかりやすいですね!
| for word in wordList: | ||
| if not word in visited and is_diff_one(current, word): | ||
| if word == endWord: | ||
| return distance+1 |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
mt2324/leetcode#2 (comment)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
問題: https://leetcode.com/problems/word-ladder/description/
次の問題:https://leetcode.com/problems/search-insert-position/description/
言語:Python3