-
Notifications
You must be signed in to change notification settings - Fork 0
Create AddTwoNumbers.md #6
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?
Conversation
l1 = l1.next | ||
l2 = l2.next | ||
|
||
return sentinental |
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.
番兵は、端っこの処理をまとめるために形式的に現れるノードを指すので、そのまま返しているということは番兵ではないと思われます。
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここのところは、入力の ListNode を変更していますね。
これがライブラリーだったとして、他の人が
c = addTwoNumbers(a, b)
と呼んで、a, b が変更されたらそれなりに驚くと思うのです。
変更していいかは状況次第ですが、呼び出す人のことを考えてみましょう。(考えた上で決行するのはあり。)
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.
l1, l2 に代入しているだけならば、呼び出し元に影響はないので、
l1 = l1.next
l2 = l2.next
とまとめるのは一つです。
l2 = l2.next | ||
|
||
return sentinental | ||
``` |
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.
この場合分けができているのは、とてもよいと思います。
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.
読みやすくていいと思います。step 1のcarryなしの解法でも場合分けがちゃんと考えられていて面白かったです
def addTwoNumbers( | ||
self, l1: Optional[ListNode], l2: Optional[ListNode] | ||
) -> Optional[ListNode]: | ||
sentinental = ListNode(0, None) |
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.
sentinelでしょうか
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
step 2, 3の様に、ただvalを見ていき、nextで次に進むだけであるなら、書き換えられるわけではないので、直で動かすこと自体に問題はないと思います。
ただ、この問題では「ある桁のノードに格納されている値」に注目してくところがあるので、nodeをおいたほうが自然かなくらいで好みの問題だと思いました。
次に解く問題:https://leetcode.com/problems/valid-parentheses/description/