Open
Conversation
oda
reviewed
Jan 2, 2025
| l1 = l1.next | ||
| l2 = l2.next | ||
|
|
||
| return sentinental |
There was a problem hiding this comment.
番兵は、端っこの処理をまとめるために形式的に現れるノードを指すので、そのまま返しているということは番兵ではないと思われます。
oda
reviewed
Jan 2, 2025
Comment on lines
+31
to
+34
| if l1.next is None and l2.next: | ||
| l1.next = ListNode(0, None) | ||
| elif l1.next and l2.next is None: | ||
| l2.next = ListNode(0, None) |
There was a problem hiding this comment.
ここのところは、入力の ListNode を変更していますね。
これがライブラリーだったとして、他の人が
c = addTwoNumbers(a, b)と呼んで、a, b が変更されたらそれなりに驚くと思うのです。
変更していいかは状況次第ですが、呼び出す人のことを考えてみましょう。(考えた上で決行するのはあり。)
There was a problem hiding this comment.
l1, l2 に代入しているだけならば、呼び出し元に影響はないので、
l1 = l1.next
l2 = l2.nextとまとめるのは一つです。
oda
reviewed
Jan 2, 2025
| l2 = l2.next | ||
|
|
||
| return sentinental | ||
| ``` |
t0hsumi
reviewed
Jan 3, 2025
t0hsumi
left a comment
There was a problem hiding this comment.
読みやすくていいと思います。step 1のcarryなしの解法でも場合分けがちゃんと考えられていて面白かったです
| def addTwoNumbers( | ||
| self, l1: Optional[ListNode], l2: Optional[ListNode] | ||
| ) -> Optional[ListNode]: | ||
| sentinental = ListNode(0, None) |
Owner
Author
There was a problem hiding this comment.
sentinelですね...。ありがとうございます。
| - If l1 is None else 0という書き方は便利:https://github.com/olsen-blue/Arai60/pull/5/file | ||
| - If l1:~ sum+=, If l2:~ sum+=という書き方が読んでて綺麗だと思ったのでこの方向を目指したい | ||
| - Get_valueでNoneを0に置き換えるのはうまいと思った。条件分けが減って読みやすい。:https://github.com/t0hsumi/leetcode/pull/5/files | ||
| - l1やl2を直で動かすべきではない |
There was a problem hiding this comment.
step 2, 3の様に、ただvalを見ていき、nextで次に進むだけであるなら、書き換えられるわけではないので、直で動かすこと自体に問題はないと思います。
ただ、この問題では「ある桁のノードに格納されている値」に注目してくところがあるので、nodeをおいたほうが自然かなくらいで好みの問題だと思いました。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
次に解く問題:https://leetcode.com/problems/valid-parentheses/description/