Skip to content

Arai60/2#7

Open
liruly wants to merge 2 commits intomainfrom
Arai60/2
Open

Arai60/2#7
liruly wants to merge 2 commits intomainfrom
Arai60/2

Conversation

@liruly
Copy link
Copy Markdown
Owner

@liruly liruly commented Jan 12, 2026

2. Add Two Numbers


l2 = l2->next;
}
if (carryUp == true) {
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 (carryUp) {

で十分伝わると思います。

偽であることを確認する際、 ! 演算子だと読みにくいため、 if (false == carryUp) と書くようにしているという方を見たことがありますが、少数派のように思います。

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.

boolであればご提案の形で十分ですね。ありがとうございます。
intで扱っている場合には可読性の面から注意が必要かもしれないと感じました。

node->next = new ListNode(sum % 10);
node = node->next;

carryUp = sum/10;
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.

空ける方針で統一しようと思います。ありがとうございます。

Copy link
Copy Markdown

@xbam326 xbam326 left a comment

Choose a reason for hiding this comment

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

所用時間や知っていたことと調べたことが分けて書いてあるのが良いなと思いました。

## Step3 リファクタリング
- 所要時間: 10min
- 方針:
- boolではなくintにしてif文を減らす
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文を減らす以外に、3つ以上の数字を足すなどの拡張を考えた際に繰り上げが0or1でなくなるのでboolよりもintの方が良さそうだと私は判断しました。

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.

その観点は確かになかったです。ご指摘ありがとうございます。
boolは内部的には0か非ゼロでしかないので、3以上の状態をとるときはint、2つの状態しかとらないときは可読性を考慮して都度選択、がいいと思いました。今回はintでよかったですね。


while (l1 != nullptr || l2 != nullptr || carryUp == true) {
const int x = (l1 != nullptr) ? l1->val : 0;
const int y = (l2 != nullptr) ? l2->val : 0;
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.

pythonにはない記法ですよね。
シンプルになるけど、可読性という意味で、競プロ経験者やつよつよさん以外には読みにくいと思うので考えどころです。

- constへの代入、単純なA/B選択などで可読性を落とさず使える
- boolのメリットデメリット
- メリット: 可読性が高い
- デメリット: 処理のためにif文を介さないといけないため実行速度が落ちる
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.

確かに!!!ありがとうございます
実際最適化の段階で何がどうなっているのか、雰囲気でしかわかっていないので調べてみます。

@oda
Copy link
Copy Markdown

oda commented Jan 14, 2026

「Arai60/2」というタイトルに Add Two Numbers という問題名を足しておくと Discord 内で検索されやすくなって他の人が助かるだろうと思います。

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