Skip to content

2. add two numbers#5

Open
maeken4 wants to merge 2 commits into
mainfrom
2.-Add-Two-Numbers
Open

2. add two numbers#5
maeken4 wants to merge 2 commits into
mainfrom
2.-Add-Two-Numbers

Conversation

@maeken4
Copy link
Copy Markdown
Owner

@maeken4 maeken4 commented May 25, 2025

while (l1 || l2 || carry) {
// l1, l2がnullptrだった場合0ノードを一時的に設定する。このブロックを抜けると解放される。
ListNode fake(0);
l1 = l1 ? l1 : &fake;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ぶら下がりでも良ければ下の方が意図がはっきりしていませんか?

l1 = l1 ? l1 : &fake;

if (!l1) l1 = &fake;

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.

どちらが分かりやすいかと言われれば後者ですね…コードゴルフでもないので人が読むコードでは避けようと思います。

ListNode* tail = &dummy;
int carry = 0;

ListNode fake = ListNode(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.

外に出すならば const をより付けたいですね。

他の人のコードを見る。
- [ryosuketcさんのコード](https://github.com/ryosuketc/leetcode_arai60/blob/2_add_two_numbers/2_add_two_numbers/step3.py)と本質的に同じだがこちらのほうが丁寧に関数や定数を切り出している。
- 他の演算も実装する必要があったら補助関数を設定すると思う。
- 他のコメントでも三項演算子は避けられているようだが、今回のようなnullでないことだけ判定するくらいなら左から右に読みやすいと思うので許されたい…
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Python の三項演算子が A if condition else B というもので分かりにくいんですよね。

C++ はまだましだと思いますが、しかし、使ってきれいになることは実は少ないと思います。

ListNode* tail = &dummy;
int carry = 0;
while (l1 != nullptr || l2 != nullptr || carry != 0) {
int val = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + carry;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

下の方の2行と合わせて、これも候補でしょう。(どちらがいいというわけではなく。)

int val = carry;
if (l1) {
    val += l1->val;
    l1 = l1->next;
}
if (l2) {
    val += l2->val;
    l2 = l2->next;
}

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.

ありがとうございます。こちらも意図がわかりやすいですね。

ListNode* tail = &dummy;
int carry = 0;
while (l1 != nullptr || l2 != nullptr || carry != 0) {
int val = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + carry;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

好みの問題ですが、個人的にはこちらの方が読みやすいと思いました。

int digit1 = l1 ? l1->val : 0;
int digit2 = l2 ? l2->val : 0;
int val = digit1 + digit2 + carry;

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

@ryosuketc ryosuketc left a comment

Choose a reason for hiding this comment

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

LGTM です!

ListNode* tail = &dummy;
int carry = 0;
while (l1 || l2 || carry) {
// l1, l2がnullptrだった場合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.

なるほど、こうした fake を置くという発想はありませんでした (私も if (!l1) l1 = &fake; の方が読みやすいというのには賛成です)。もうちょっと descriptive な名前でもいいかなと思いましたが、用途が限定的なのでよいですかね。

…と思ってよい名前を少し考えましたが意外に思いつきませんでした。default を一瞬考えたけど予約語っぽいなーと思ったら本当に予約語でした: https://en.cppreference.com/w/cpp/keywords/。

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