Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions 82. Remove Duplicates from Sorted List II.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,72 @@ class Solution {

```

//時間がかかりすぎているので、ここで一旦終了
//ダミーの役割を理解するのに時間がかかった。一つずつの挙動の確認

###Step2

83. で指摘のコメントがあったが、deleteがないため、リークに注意が必要
LeetCodeでは解放不要ですが、deleteを意識したものへ変更。

```cpp
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode dummy(0, head);
ListNode* node = &dummy;
ListNode* next_node = head;

while (node && next_node) {
bool has_duplicates = next_node->next && next_node->val == next_node->next->val;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらに関しても変数に置かなくてもいいのかなと思いました。


if (has_duplicates) {
int dup_val = next_node->val; // 重複部分をすべて delete
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

次のNodeがもつ値を変数に置かなくてもいいのかなと感じました。

while (next_node && next_node->val == dup_val) {
ListNode* temp = next_node;
next_node = next_node->next;
delete temp;
}
node->next = next_node; // 重複をスキップ
}
else {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elseで改行されていると少し読み辛いと感じました。
参考にこの辺りのガイドを共有いたします。
https://google.github.io/styleguide/cppguide.html#Formatting_Looping_Branching

Copy link
Copy Markdown

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/1366778718705553520/1377689037208289471

スタイルガイドについてはコメント集に少し書いたかと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ryotaro25
スタイルの共有ありがとうございます。
私のスタイルは、提示いただいたgoogleのものを参考に基準を養っていきたいと思ってましたが、確認できていませんでした。頂くコメントを予測して、振り返ることを忘れずにしたいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oda
コメントありがとうございます。
こちらのdiscord確認できていませんでした。レビューの仕方と見るべきポイント確認しておきます。

node = next_node;
next_node = next_node->next;
}
}
return dummy.next;
}
};

```

他の方のGitHubを確認
*https://github.com/maeken4/Arai60/pull/4/commits/32508ad2702b7c64d55f45023bfd816fa33358c3
 maeken さん 
 野田さんのコメント:std::unique_ptr や std::shared_ptr の使い方を確認:https://cpprefjp.github.io/reference/memory/unique_ptr.html
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご存知でしたらすみません。Effective C++にこの辺りのことがいくつか書かれております。
この会でも何度か紹介されておりますので共有いたします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ryotaro25
コメントありがとうございます!
リンクが添付されてないみたいです。お手隙で共有頂けたらと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すみませんこちらは書籍になります🙇
https://amzn.asia/d/1fDsGyd


* https://github.com/kazukiii/leetcode/pull/5/commits/6f0b47e756e954af07f99dad8a37c3d99c8ef65a
野田さんのコメント:early return したほうが読みやすくなると思います。

```cpp
if (curr->val == curr->next->val) {
curr->next = curr->next->next;
skip_count++;
continue;
}

if (skip_count == 0) {
prev = curr;
} else {
prev->next = curr->next;
}

skip_count = 0;
curr = curr->next;
```

###Step3
繰り返し

【感想】
理解するのに時間がかかった。
ネストが深くなると読みづらくなるので、early return をした書き方の練習。