Conversation
| if not head: | ||
| return Non | ||
| slow = head | ||
| fast = head | ||
|
|
||
| while fast and fast.next: |
There was a problem hiding this comment.
slow と fast はその下の while 文から参照されるので、以下のようにした方が自然に感じました。
| if not head: | |
| return Non | |
| slow = head | |
| fast = head | |
| while fast and fast.next: | |
| if not head: | |
| return Non | |
| slow = head | |
| fast = head | |
| while fast and fast.next: |
There was a problem hiding this comment.
whileにつなげることで処理の関係性を暗に示せると解釈しました。確かにそのほうがブロックとしてまとまっていて自然と感じました。
|
|
||
| ```python | ||
| class Solution: | ||
| def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: |
There was a problem hiding this comment.
再帰での書き換えはよく求められるそうなので、そちらでも書けるようにしておくといいと思います。
| if temp in visited: | ||
| return temp |
There was a problem hiding this comment.
疑問点:
- setでもリストでもとおったが、どっちのほうがよいのだろうか?
まずハッシュセットとリストの違いを調べることをおすすめします。
もじ調べてみてピンとこなかったら、この部分の時間計算量を比べてみることを合わせておすすめします。
There was a problem hiding this comment.
ハッシュセットが重複なしで要素を持つのに対しリストは重複許容・順序をもつこと程度は理解していましたが、速度に違いがあることは調べて理解できました。
※以下、自分用
- 要素の存在確認においてはハッシュセットのほうがリストのほうが早い。
- リストは最悪全ての要素を確認する(O(n) となる)のに対し、ハッシュセットは要素のハッシュ値を計算して直接位置にアクセスするのでO(1)ですむ。
| if not head: | ||
| return None | ||
|
|
||
| temp=head |
There was a problem hiding this comment.
個人的にはtempという変数名はスコープが狭くて一時保存であることを強調したいときのみにするのが良いと思います。
node とか current をよく見かけます。
There was a problem hiding this comment.
current は他の方のPRで小田さんが違和感をあるとコメントしてくださっていたのを見て避けていました。(リンク)
おっしゃるとおりtempも命名的には微妙ですね、素直にnodeがいいかなと思いました。
| return None | ||
|
|
||
| temp_node=head | ||
| visited=set() #setでもOKだったがどっちがよいのか? |
There was a problem hiding this comment.
setは検索が average O(1) で出来ますね。
https://docs.python.org/3/tutorial/datastructures.html#sets
There was a problem hiding this comment.
ドキュメントリンク送付ありがとうございます。普段からここの参照を意識できるようにします。
| ```python | ||
| class Solution: | ||
| def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
| if not head: |
| fast = fast.next | ||
| slow = slow.next |
There was a problem hiding this comment.
この時点ではfastとslowはもはや同じスピードで動いているので変数名を変えてあげると良さそうです。
There was a problem hiding this comment.
実態と変数がずれていて違和感があると解釈しました。別変数で用意します。
…etcode into linked_list_cycle_ii
| if not head: | ||
| return None | ||
|
|
||
| temp=head |
There was a problem hiding this comment.
= の両隣に空白を空けることをお勧めいたします。
https://peps.python.org/pep-0008/#other-recommendations
Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not).
https://google.github.io/styleguide/pyguide.html#s3.6-whitespace
Surround binary operators with a single space on either side for assignment (=), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), and Booleans (and, or, not). Use your better judgment for the insertion of spaces around arithmetic operators (+, -, *, /, //, %, **, @).
Arai60/LinkedList/answer.md
Outdated
|
|
||
| while temp_node: | ||
| if temp_node in visited: | ||
| visited.add(temp_node) |
There was a problem hiding this comment.
インデントがずれているように思います。このコードは文法エラーになりませんでしょうか…。
There was a problem hiding this comment.
おっしゃる通りずれてました、転記の際に少し手直ししたせいでした
| return None | ||
| ``` | ||
|
|
||
| - フロイドの循環検出法を実装したがTLE。解法をみた(下記は通らなかった実装) |
There was a problem hiding this comment.
フロイドの循環検出法は、なぜか知っている人が多いのですが、想定解や、期待されている解ではないと思います。 set を使ったほうが想定解だと思います。
There was a problem hiding this comment.
違う視点での解法を試す意図で、前回実装したこともありこちらを試していました。
setを使った解法が想定解だと思われる理由は何でしょうか?(個人的に、どちらがぱっと思いつくかといわれるとsetのほうな気はしています)
There was a problem hiding this comment.
setを使った解法が想定解だと思われる理由は何でしょうか?(個人的に、どちらがぱっと思いつくかといわれるとsetのほうな気はしています)
フロイドの循環検出法が、ソフトウェアエンジニアの常識に含まれないと感じるためです。
There was a problem hiding this comment.
ありがとうございます。正直自身に常識がインストールされていないせいか、まだ完全に線引きが言語化できていないですが一旦そのまま受け止めてみます。
問題:https://leetcode.com/problems/linked-list-cycle-ii/
考えたこと:https://github.com/ask-1407/leetcode/blob/linked_list_cycle_ii/Arai60/LinkedList/answer.md
次に解く予定:https://leetcode.com/problems/remove-duplicates-from-sorted-list/