Skip to content

Re: 2.Add Two Numbers#15

Open
philip82148 wants to merge 1 commit into
mainfrom
re2.Add-Two-Numbers
Open

Re: 2.Add Two Numbers#15
philip82148 wants to merge 1 commit into
mainfrom
re2.Add-Two-Numbers

Conversation

@philip82148
Copy link
Copy Markdown
Owner

こちらは以前解いたものの解きなおしになります

前回のPRはこちらです: #2

問題

https://leetcode.com/problems/search-insert-position/description/

補足

<番号>r.cpp<番号>.cppを書きなおしたものになっています。(微妙に違うか同じです)

public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int carry = 0;
ListNode sentinel, *node = &sentinel;
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://google.github.io/styleguide/cppguide.html#Pointer_and_Reference_Expressions

It is allowed (if unusual) to declare multiple variables in the same declaration, but it is disallowed if any of those have pointer or reference decorations. Such declarations are easily misread.

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.

なるほどです!ありがとうございます!

public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int carry = 0;
ListNode sentinel, *node = &sentinel;
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行にしますね。

sum += l2->val;
l2 = l2->next;
}
node = node->next = new ListNode(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.

この記法知りませんでしたが,とても便利ですね.参考になります.

carry = sum / 10;
}
return sentinel.next;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

以前解かれていた 5.cpp のファイルには空行がいくつかありましたが、それらをなくしたこちらのコードの方が読みやすく感じました。

@Miyamoto-tryk
Copy link
Copy Markdown

読みやすくて良いと思います!

// <時間>
// 5分
// <感想>
// 久しぶりに書き直したら5.cppとほぼ同じものが出来上がった。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

シンプルにまとまっていて読みやすかったです

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.

7 participants