-
Notifications
You must be signed in to change notification settings - Fork 0
Arai60/142 Linked List Cycle II #2
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?
Conversation
|
||
疑問点: | ||
|
||
- submit すると 8/17 成功、時間の問題ではないので、何かのミスです。 |
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.
動かないテストケースを見つけてきて、コードを追ってください。
書くよりも読む能力のほうが大事です。
疑問点: | ||
|
||
- submit すると 8/17 成功、時間の問題ではないので、何かのミスです。 | ||
- slow pointer と fast pointer を使用するところまで気づいたが、floyd の tortoise and hare アルゴリズムをきちんと知らなかったため、解決できませんでした。 |
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 | ||
|
||
# Step1 |
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.
(odaさんのコメントと同じかもしれませんが、)一旦正解するまでをSTEP1とすると、他の方々と認識が揃って良さそうだと思いました。
新井さんの問題順番にやっていきます。一日一問で最初はいいでしょう。答えを見ずに考えて、5分考えて分からなかったら答えを見てください。答えを見て理解したと思ったら、答えを隠して書いてください。筆が進まず5分迷ったら答えを見てください。そして、見ちゃったら一回全部消してやり直しです。答えを送信して、正解になったら、まずは一段階目です。
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 は自分が回答を見ずに書いたコードをそのまま書くだけと思っていました。このコードが全てのtest 通らなかったが、20分以上たったので、正解を見てみました。それこのコードに戻らなかったです。時間取られすぎるとあんまり効率悪いと思った追わなかったです。
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/1227462534211309589/1237419839795101858
https://github.com/h1rosaka/arai60/pull/4/files
このあたりを参考に何をしようとしているかを考えていただけると。
|
||
class Solution: | ||
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
nodes_passed_by_slow = 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.
このコードでどのような処理を行いたかったか、順を追って説明してみていただけますか?
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.
アルゴリズムの手順:
2つの集合の使用:
nodes_passed_by_slow: slowポインタが通過したノードを記録するためのセット。
nodes_passed_by_fast: fastポインタが通過したノードを記録するためのセット。
ポインタの移動:
slowポインタは1つずつ進みます。
fastポインタは2つずつ進みます。
各ステップで、slowとfastが通過したノードをそれぞれのセットに追加します。
サイクルの検出:
もしslowとfastが同じノードに到達した場合、サイクルが存在することになります。この場合、nodes_passed_by_slowとnodes_passed_by_fastの交差部分を求め、その最初のノードをサイクルの開始地点として返そうとしています。
サイクルが見つからない場合:
fastがリストの終端に到達した場合、サイクルが存在しないと判断して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.
ありがとうございます。おっしゃる通り、セットの交差部分が彼方らうサイクルの開始地点になるとは限らないと思います。問題点を自力で見つけることができたのは、とても素晴らしいと思います。
https://github.com/codgas/leetcode/pull/2/files#r1717036142
の方法で解いてみていただけますか?
|
||
class Solution: | ||
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: | ||
slow = fast = 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.
floyd の tortoise and hare アルゴリズムでも解けるのですが、このアルゴリズムはソフトウェアエンジニアの常識には含まれていないように思います。おそらく想定解は、過去に辿ったノードを set に入れていき、今見ているノードと同じノードが set に含まれていたら、それを返す、というものだと思います。この方針の回答も書いてみていただけますか?
問題: 142. Linked List Cycle 2
https://leetcode.com/problems/linked-list-cycle-ii/description/
言語: Python