Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Create AddTwoNumbers.md #6

wants to merge 2 commits into from

Conversation

SanakoMeine
Copy link
Owner

l1 = l1.next
l2 = l2.next

return sentinental
Copy link

Choose a reason for hiding this comment

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

番兵は、端っこの処理をまとめるために形式的に現れるノードを指すので、そのまま返しているということは番兵ではないと思われます。

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)
Copy link

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 が変更されたらそれなりに驚くと思うのです。

変更していいかは状況次第ですが、呼び出す人のことを考えてみましょう。(考えた上で決行するのはあり。)

Copy link

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
```
Copy link

Choose a reason for hiding this comment

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

この場合分けができているのは、とてもよいと思います。

Copy link

@t0hsumi t0hsumi left a 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)
Copy link

Choose a reason for hiding this comment

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

sentinelでしょうか

Copy link
Owner Author

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を直で動かすべきではない
Copy link

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をおいたほうが自然かなくらいで好みの問題だと思いました。

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.

3 participants