-
Notifications
You must be signed in to change notification settings - Fork 0
Create 141. Linked List Cycle.md #1
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: main
Are you sure you want to change the base?
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,107 @@ | ||
| 問題文: https://leetcode.com/problems/linked-list-cycle/description/ | ||
|
|
||
| # step1: 何も見ないで考える | ||
| - 空間計算量がO(1)になる方法は思いつかなかったので、素直にsetを使った方法で書いた。 | ||
| - 今回の方法の時間計算量はO(n)、空間計算量はO(n)。 | ||
| - Noneはシングルトンだから判定に使う演算子は'=='でなく'is'である、という事実だけを特に理解せず記憶していたのでついでに理由を調べてみた。 | ||
| - 'is' はオブジェクトの同一性を判定するための演算子。 | ||
| - '==' は値の比較のための演算子で、x == y は x.__eq__(y) という特殊メソッドを呼び出している。 | ||
| - __eq__() はデフォルトだと is と同じだが、カスタマイズ可能。 | ||
| - シングルトンが何なのかよく分からなかったが、絶対に1個のインスタンスしか持たないオブジェクトみたいなものだと理解しておく。 | ||
| - 結局、Noneオブジェクトには絶対に1つしかインスタンスがないんだからオブジェクトの同一性をisで判定すれば十分、ということか。よくわからん。\ | ||
|
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. この問題で==よりisが推奨されることが多いのは、 今回、自作(leetcode運営側の定義ではありますが)のオブジェクトを対象としているので、 今回のような小規模なコードでは大した問題ではないですが、
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. コメントありがとうございます。言語の仕様というよりは、人間側の認知不可を下げるためのルールだったのですね。私の場合、そもそもの開発経験がないのでなおのこと嬉しさが分かりにくかったのだと思います。 |
||
| https://docs.python.org/3.13/library/operator.html \ | ||
| https://docs.python.org/3.13/reference/datamodel.html#basic-customization | ||
| ```python | ||
| class Solution: | ||
| def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
| checked_nodes = set() | ||
| node = head | ||
| if node is None: | ||
| return False | ||
| while node.next is not None: | ||
| if node in checked_nodes: | ||
| return True | ||
| checked_nodes.add(node) | ||
| node = node.next | ||
|
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. while node is not None:
if node in checked_nodes:
return True
checked_nodes.add(node)
node = node.nextと書かなかった理由はありますか? 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. と思いましたが、他の方も指摘いただいているので大丈夫そうですね |
||
| return False | ||
|
|
||
| ``` | ||
|
|
||
|
|
||
| # step2: 他の人のレビューを参考にコードを整える | ||
| ## setを使った方法 | ||
| - setの名前は visited_nodes や visited としている人が多かった。visited系の名前は連結リストの上を移動しているようなイメージがして処理を頭の中で描きやすい気がするので私も採用した。 | ||
| - 連結リストのnodeを調べるのは分かってるから、setの名前にnodesは入れなくてもいいという意見もあり、もっともなのでこちらも採用。 | ||
| ```python | ||
| class Solution: | ||
| def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
| visited = set() | ||
| node = head | ||
| if node is None: | ||
| return False | ||
| while node.next is not None: | ||
| if node in visited: | ||
| return True | ||
| visited.add(node) | ||
| node = node.next | ||
| return False | ||
|
|
||
| ``` | ||
|
|
||
| ## フロイドの方法 | ||
| - 新井さんの解説動画で知った。通常人が初見で思いつける方法ではないと思ったのでDiscordの履歴を調べたら「科学手品」みたいなものということだったので一安心。\ | ||
| https://discord.com/channels/1084280443945353267/1195700948786491403/1195944696665604156 | ||
| - ループの条件で少し手こずったが、ウサギはノードを1つ飛ばして移動するので到着予定である2つ先のノードが存在してるかまでチェックすると整理した。 | ||
| ```python | ||
| class Solution: | ||
| def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
| fast = head | ||
| slow = head | ||
| if fast is None: | ||
| return False | ||
| while fast.next is not None and fast.next.next is not None: | ||
|
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. ここもですね、while fast is not None and fast.next is not Noneでなかった理由が気になります👀 |
||
| fast = fast.next.next | ||
| slow = slow.next | ||
| if fast is slow: | ||
| return True | ||
| return False | ||
|
|
||
| ``` | ||
|
|
||
|
|
||
| # step3: 10分以内にエラーを出さずに3回書く | ||
| ## setを使った方法 | ||
| ```python | ||
| class Solution: | ||
| def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
| visited = set() | ||
| node = head | ||
| if node is None: | ||
| return False | ||
| while node.next is not None: | ||
|
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. 確かにおっしゃる通りですね。全然気が付いてなかったです。コードの条件の整理はかなり苦手のようなので改善できるよう精進します。 |
||
| if node in visited: | ||
| return True | ||
| visited.add(node) | ||
| node = node.next | ||
| return False | ||
|
|
||
| ``` | ||
|
|
||
| ## フロイドの方法 | ||
| ```python | ||
| class Solution: | ||
| def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
| fast = head | ||
| slow = head | ||
| if fast is None: | ||
|
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 fast is None: の部分は、
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. 確かにおっしゃる通りですね。指摘されるまで気づいていなかったので、コードの整理はかなり課題がありそうです。意識的に改善できるよう精進します。 |
||
| return False | ||
| while fast.next is not None and fast.next.next is not None: | ||
| fast = fast.next.next | ||
| slow = slow.next | ||
| if fast is slow: | ||
| return True | ||
| return False | ||
|
|
||
| ``` | ||
|
|
||
| 次の問題: 142. Linked List Cycle II ( https://leetcode.com/problems/linked-list-cycle-ii/description/ ) | ||
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.
それでいいかと思います。
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のドキュメントで解決しなかったのでwikipediaなどを参考にしました。ただ、ひらがなの読み書きを練習している段階で真剣に取り組むべき話題でもなさそうなのでアドバイス通り先に進むことにします。
https://ja.wikipedia.org/wiki/Singleton_%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3