-
Notifications
You must be signed in to change notification settings - Fork 0
Re: 2.Add Two Numbers #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #if __has_include("../../debug.hpp") | ||
| #include "../../debug.hpp" | ||
| #endif | ||
| // ここまでローカルでのデバッグ用なので気にしないでください -------------------- | ||
|
|
||
| using namespace std; | ||
|
|
||
| struct ListNode { | ||
| int val; | ||
| ListNode* next; | ||
| ListNode() : val(0), next(nullptr) {} | ||
| ListNode(int x) : val(x), next(nullptr) {} | ||
| ListNode(int x, ListNode* next) : val(x), next(next) {} | ||
| }; | ||
|
|
||
| // <時間> | ||
| // 5分 | ||
| // <感想> | ||
| // 久しぶりに書き直したら5.cppとほぼ同じものが出来上がった。 | ||
| // <コメント> | ||
| // - 特になし。 | ||
| class Solution { | ||
| public: | ||
| ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { | ||
| int carry = 0; | ||
| ListNode sentinel, *node = &sentinel; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同一の行で値型やポインター型等の型を混ぜて宣言すると、読みづらく感じます。分けて書いたほうがよいと思います。 https://google.github.io/styleguide/cppguide.html#Pointer_and_Reference_Expressions
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なるほどです!ありがとうございます! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 私は2行にしますね。 |
||
| while (l1 || l2 || carry) { | ||
| int sum = carry; | ||
| if (l1) { | ||
| sum += l1->val; | ||
| l1 = l1->next; | ||
| } | ||
| if (l2) { | ||
| sum += l2->val; | ||
| l2 = l2->next; | ||
| } | ||
| node = node->next = new ListNode(sum % 10); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この記法知りませんでしたが,とても便利ですね.参考になります. |
||
| carry = sum / 10; | ||
| } | ||
| return sentinel.next; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 以前解かれていた 5.cpp のファイルには空行がいくつかありましたが、それらをなくしたこちらのコードの方が読みやすく感じました。 |
||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
シンプルにまとまっていて読みやすかったです