Conversation
| } | ||
| return nullptr; | ||
| } | ||
| }; No newline at end of file |
| set<ListNode*> used; | ||
| ListNode* curr = head; | ||
| while (curr != nullptr) { | ||
| if (used.count(curr)) return curr; |
There was a problem hiding this comment.
ifブロックの終わりがどこか読み辛くなるので、ブロックを用いる方が好みです。
Googleのスタイルガイドには以下のような記載もございます。
Put any controlled statements inside blocks (i.e., use curly braces).
https://google.github.io/styleguide/cppguide.html#Formatting_Looping_Branching
There was a problem hiding this comment.
コメントありがとうございます!ブロックを使った書き方をするようにします。
There was a problem hiding this comment.
Googleガイドも一例ですので、ここの会に参加されてる他の皆さんのカードを見るなり、議論を見るなりして好みや考え方を身につけていけば良いのかなと思います。
| set<ListNode*> used; | ||
| ListNode* curr = head; | ||
| while (curr != nullptr) { | ||
| if (used.count(curr)) return curr; |
There was a problem hiding this comment.
containsを使った方法もございます。
https://en.cppreference.com/w/cpp/container/set/contains.html
There was a problem hiding this comment.
containsは知らなかったです、ありがとうございます。
| @@ -0,0 +1,11 @@ | |||
| ### step1 | |||
|
|
|||
| 141. Linked List Cycleでsetによる解法でも解いてみていたので、そちらの解法と同じようにすると解けた。 | |||
There was a problem hiding this comment.
時間計算量や空間計算量についての言及があればなおいいのかなと思いました。
Linked List Cycleでsetによる解法でも解いてみていた
レビューする人によっては上記を知らない可能性があるので説明があればより良いのかなと思いました。
There was a problem hiding this comment.
時間計算量や空間計算量についてはなんとなくしか理解できていないので、少し勉強してみます。
| @@ -0,0 +1,11 @@ | |||
| ### step1 | |||
|
|
|||
| 141. Linked List Cycleでsetによる解法でも解いてみていたので、そちらの解法と同じようにすると解けた。 | |||
There was a problem hiding this comment.
Linked List Cycleでもあったフロイドの循環検出を使っても書けることは認識しておられると思うので、学習のために検討しても良いかなと思いました。
katsukii/leetcode#13 (comment)
この問題は、どちらかというと、このあたりのコードの整え方を見ておくといいかもしれません。
コードの整え方の練習にもなるので。
| public: | ||
| ListNode *detectCycle(ListNode *head) { | ||
| set<ListNode*> used; | ||
| ListNode* curr = head; |
There was a problem hiding this comment.
好みの範疇かもしれませんが、currはpreviousなどと対比して使うイメージがあるので、node等の名前の方がいいのではないかと思いました。
この問題:https://leetcode.com/problems/linked-list-cycle-ii/description/
次に解く問題:https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/