-
Notifications
You must be signed in to change notification settings - Fork 0
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
Create 142LinkedListCicleII.md #3
base: main
Are you sure you want to change the base?
Conversation
142.Linked List Cycleを解いた
else: | ||
visited_node.add(node) | ||
node = node.next |
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.
elseで書かなくてもいいのではないでしょうか?
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.
実際step2を経てそちらの方がなんとなく自然だなと感じるようになりましたが、やはりそちらの方が一般的な感覚なんだなと確信が持てました。ご指摘ありがとうございます。
node = head | ||
visited_node = set() | ||
|
||
while node and node.next is not None: |
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.
Noneとの比較に、is not
を使うものと使わないものが混在しているのが気になりました。
また、ここの部分でもwhile node
だけでも正しく動くと思います。
class Solution: | ||
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
node = head | ||
visited_node = set() |
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.
自分ならvisited
もしくはvisited_nodes
とするかなと思います。
visited_node = set() | ||
node = head | ||
|
||
while node and node.next is not None: |
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.
ここの部分はwhile node
だけでも正しく動くと思います。while node and node.next is not None:
としても、ループが一回回る回数が減るだけなので、自分ならwhile node
にするかなと思います。
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.
深く考えずに過去のPRを反映していたのに気づけました。ご指摘ありがとうございます。
- https://github.com/ichika0615/arai60/pull/2/files | ||
|
||
## Step 1 | ||
- ループが閉じる番号を探すなら順序を考慮するリストの方が良いのかなと思い、最初はリストで解いてみようとするも、計算時間オーバーでギブアップ |
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.
in の判定が、中身を頭から全部舐めて確認しているので、数がたくさんに増えるにつれて遅くなっていくんですね。
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.
なるほど。「なんとなくこの辺のせいなのかな」くらいの感度しかなかったので、もう一度見返してみます。
## Step 1 | ||
- ループが閉じる番号を探すなら順序を考慮するリストの方が良いのかなと思い、最初はリストで解いてみようとするも、計算時間オーバーでギブアップ | ||
- https://github.com/pineappleYogurt/leetCode/pull/3/files を拝見して、先の問題と同じようにsetで解いてnodeをreturnすれば良さそうと理解して書き直す | ||
- すでに解いた方々の回答と付き合わせてもそんなに悪くなさそう…?今の実力だとどんなコメントがつくか予想がつかない。 |
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.
ここまでついたような細かい表現方法を整える方法の趣味趣向以外は問題ないでしょう。
- なぜ機能するのかさっぱりわからなかったフロイドのアルゴリズムについて小田さんが解説されていた:https://discord.com/channels/1084280443945353267/1246383603122966570/1252209488815984710 | ||
- 自分では絶対に思いつかないが、お絵描きしてみると言っていることは理解できた。時間が余った時にパズル的に聞かれることはあるとのことなので、おまけ気分で試しに書いてみる | ||
- 先人のコードとレビューを見ていると、フロイドのアルゴリズムを使った場合はどうも前の問題でも言及されていたwhileの見通しが問題が生じやすそう:https://discord.com/channels/1084280443945353267/1221030192609493053/1225674901445283860 Python独自の書き方というのがよく分かっていないので、1度試しに書いてみる(Whileとelseを並列させている部分がPython独自なのかな?) | ||
- が、たいしてif文の中が長くないので他の書き方でもよかった気がする。whileを2つ並べているのがまとめられそうでなんとなく気に食わない。 |
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.
確かにfast_nodeとslow_nodeがぶつかった場合に片方のポインタを最初の位置に戻してまた一つずつ動かせということなので、二つあるwhile文を一つにまとめることはできると思います。while文の中が長くなっちゃうので読めるようにする工夫はいるかもしれません。
```Python | ||
class Solution: | ||
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
fast_node = head |
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.
この関数内で扱われる対象がNodeであることは明らかであるため、わざわざnodeまで書かなくてもいいのではと思いましたがいかがでしょう?
katataku/leetcode#2 (comment)
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.
なるほど、ありがとうございます
else: | ||
return None | ||
|
||
node_from_start = head |
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.
上同様ここもnodeであることは明らかで、使用する範囲も数行なのでstartなどでもいいかなと思いました(startだとちょっと汎用的すぎるかもですが)
142.Linked List Cycleを解いた