Skip to content

Conversation

oshibusu
Copy link
Owner

解いた問題
https://leetcode.com/problems/linked-list-cycle-ii/

次回の問題
https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/

先ほどのPRは141のcommitも含んでいたので、クローズして、新しいPRを出しました

自分がサクッと解けたと感じたのは、あくまでも直前に141のset型を用いた解法を学んだからである。その流れですぐこの問題は解けた。
two pointerの解答は、その場で思いつくのは中々難しいと思う。
- 採用側の意図は、141と同じく"set使えますよね?"という確認なのだと思われる。
- 勿論、それでsetではなくtwo pointerの解答を書けたらそれはそれで知識があって良しor頭の回転が早くて良し、という感じ。
Copy link

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
Copy link

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:
Copy link

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される。
Copy link

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

Copy link
Owner Author

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使えますよね?"という確認なのだと思われる。

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:
```
の方がより正確なのでは無いかと思った

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:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants