-
Notifications
You must be signed in to change notification settings - Fork 0
142-Linked-List-Cycle-IIを解いた #3
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: master
Are you sure you want to change the base?
Conversation
自分がサクッと解けたと感じたのは、あくまでも直前に141のset型を用いた解法を学んだからである。その流れですぐこの問題は解けた。 | ||
two pointerの解答は、その場で思いつくのは中々難しいと思う。 | ||
- 採用側の意図は、141と同じく"set使えますよね?"という確認なのだと思われる。 | ||
- 勿論、それでsetではなくtwo pointerの解答を書けたらそれはそれで知識があって良しor頭の回転が早くて良し、という感じ。 |
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.
いや、私は、知識があってよいとも、頭の回転が速くてよいとも評価しませんね。
|
||
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.
私は、一行に書くのはあまり好みません。趣味の範囲ですかね。
return None | ||
|
||
slow = head | ||
while slow != fast: |
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.
!= は __ne__ が呼ばれます。is not とどちらがいいかは状況次第ですが、何が起きるかは認識しましょう。
else: | ||
return Y | ||
|
||
こういう書き方ができるのが美しいと思った。同様の書き方で、for文でno break occuredの時でも、else clauseがexcuteされる。 |
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://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.9kpbwslvv3yv
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.
「コードの整え方」は一部見ました。全ては確認していなかったので、今全部読んでみます。
|
||
自分がサクッと解けたと感じたのは、あくまでも直前に141のset型を用いた解法を学んだからである。その流れですぐこの問題は解けた。 | ||
two pointerの解答は、その場で思いつくのは中々難しいと思う。 | ||
- 採用側の意図は、141と同じく"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.
レベルにもよりますが、前提としてまず自分が書いたコードの意味を説明できるか・どういう意図を持ってコードを書いているかということの確認があると思います。
そのうえで、たとえばlistだとどういう事が起こるか、あとはコードを扱う技術についてsetを使う場合でreturnを一つにするとしたらどう書き換えられるかとかも対話では見ることができます。
(この場合は書き換えると若干わかりにくくなると思いますが、技術としてそういうのができるかどうか。できないと常にNGとかではないです。)
``` | ||
while slow is not fast: | ||
``` | ||
の方がより正確なのでは無いかと思った |
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.
正確というより確実、という感じだと思います。
isはオーバーロード出来ませんが__eq__は出来るので。
if fast == slow: | ||
break | ||
|
||
if fast is None or fast.next is 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 fast and fast.nextとしているので、統一感があった方が良いと思いました。
好みの問題かもしれませんが自分は面倒でもNoneの判定はisで行うようにしています。
https://google.github.io/styleguide/pyguide.html#2144-decision
解いた問題
https://leetcode.com/problems/linked-list-cycle-ii/
次回の問題
https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/
先ほどのPRは141のcommitも含んでいたので、クローズして、新しいPRを出しました