Skip to content

2. Add Two Numbers#6

Open
kazukiii wants to merge 4 commits intomainfrom
arai60/add-two-numbers
Open

2. Add Two Numbers#6
kazukiii wants to merge 4 commits intomainfrom
arai60/add-two-numbers

Conversation

@kazukiii
Copy link
Copy Markdown
Owner

@kazukiii kazukiii commented Jun 9, 2024

問題へのリンク
https://leetcode.com/problems/add-two-numbers/description/

次に解く問題
20. Valid Parentheses

README.mdへ頭の中の言語化と記録をしています。

int carry = 0;
ListNode dummy_head;
ListNode* node = &dummy_head;
while (list2 || list1) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

while (list1 || list2)の方が違和感ないと思いました🙇

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.

気付きませんでした。ご指摘ありがとうございます。

- time: O(n), space: O(n)
- 繰り上がりが最後に残った場合のみ注意
- リストの走査は繰り返しでも再帰でもOK
- 再帰にするメリットは思いつかないので繰り返しでいく
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

再帰に関しても時間/空間計算量やコードに落とす際の観点も踏まえた方が
なぜ @kazukiii さんが今回の実装を選ばれたのかがより伝わると思いました。
よく自分が受けている指摘ですが🙇

Copy link
Copy Markdown
Owner Author

@kazukiii kazukiii Jun 10, 2024

Choose a reason for hiding this comment

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

レビューありがとうございます。なぜ選択したのかも言語化して残すようにしてみます。

以下、今回の問題に関して考えることです。
再帰に関しては実装方法の違いだと考えており、時間/空間計算量は同じになります。
再帰処理に関しては、再帰的な関数呼び出しや呼び出しごとに生成されるスタックフレームを考えると、時間的にも空間的にもオーバーヘッドが多少あるため、今回繰り返しを選択しました。

一応再帰でも書いてみました->c9ba9b4

- 新しいノードをスタック領域に確保するとスコープを抜けた時点でメモリが解放されるのでうまくいかない
- ヒープ領域にオブジェクトを作成したけど、このオブジェクトの管理は誰の責務になるのかがわかっていない
- C++実務で使ったことないが、実務だとどうやるんだろう
- Linked Listのポインタをスマートポインタにすればよさそう? -> 参照が切れた時点で自動的にメモリが解放されそう
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

ありがとうございます。理解が深まりました。

  • 動的に確保されたオブジェクトには所有者を設定する
  • その所有者がオブジェクトのメモリ管理に関する責任を持つ
  • 基本的には、所有権はuniqueにする(std::unique_ptrを使う)
  • 以上を元に実務では以下のようにすればいいと考えました
    • Linked Listのポインタをstd::unique_ptrで定義する
    • std::make_uniqueでスマートポインタを生成
    • Linked Listに追加する際に、所有権をmoveする

class Solution {
public:
ListNode* addTwoNumbers(ListNode* list1, ListNode* list2) {
ListNode dummy_head;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

一応、dummy を使わない方法も書いてみますか?
(足すときに条件分岐します。)

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.

dummyを使わない方法も書いてみました。-> a9c7637

@ryoooooory
Copy link
Copy Markdown

いいとおもいます!

@rihib rihib mentioned this pull request Aug 6, 2024
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.

5 participants