-
Notifications
You must be signed in to change notification settings - Fork 0
2-add-two-numbers #13
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
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,13 @@ | ||
| ## step1で考えたこと | ||
|
|
||
| 和が10以上になった時にどう繰り上げを行うか。 | ||
|
|
||
| dummyというか結果を格納するList Nodeは必要そう | ||
|
|
||
| module 10 でのあまりをNodeの値として使えそう | ||
|
|
||
| ## step1を振り返って | ||
| 問題を解く方針はだいたいあっていたが、コードの細かい書き方や、まだLinked Listの書き方がイマイチになっていることに気づいた。 | ||
|
|
||
| 今回の場合、和の一の位の数をNodeの後ろにどんどん追加していく必要があるからtailを使う. | ||
| もし、問題の特性が、Nodeを前に前に追加していく場合だと、今度はtailではなくheadを使って操作していく。 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| result = ListNode(0) | ||
| tail = result | ||
| list1 = l1 | ||
| list2 = l2 | ||
| carry = 0 | ||
|
|
||
| while list1 or list2 or carry: | ||
| x = list1.val if list1 else 0 | ||
| y = list2.val if list2 else 0 | ||
|
|
||
| total = x + y + carry | ||
| carry = total // 10 | ||
| digit = total % 10 | ||
|
|
||
| tail.next = ListNode(digit) | ||
| tail = tail.next | ||
| if list1: | ||
| list1 = list1.next | ||
| if list2: | ||
| list2 = list2.next | ||
| return result.next | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # 動かないコード | ||
|
|
||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| result = ListNode(0, l1) | ||
| list1 = l1.val | ||
| list2 = l2.val | ||
| res = 0 | ||
|
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. res が residual の略なのか、 result の略なのか、一瞬判断に迷いました。 以下のコメントをご参照ください。
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. 参考リンクの提供ありがとうございます! |
||
|
|
||
| while list1 or list2: | ||
| if res > 10: | ||
| res = list1.val + list2.val + 1 | ||
| else: | ||
| res = list1.val + list2.val | ||
| if res >= 10: | ||
| result.next = res % 10 | ||
| else: result.next = res | ||
| list1 = list1.next | ||
| list2 = list2.next | ||
| return result.next | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # step2のコードをもう少し簡潔に書いたVersion | ||
|
|
||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| dummy = ListNode() | ||
|
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. dummy に番兵ノードを代入しているにもかかわらず、 dummy を先に進めていっている点に違和感を感じました。 dummy の値は動かさず、別のポインターを動かしていったほうが自然に感じられます。 dummy = ListNode()
node = dummy
...
while l1 or l2 or carry:
...
node.next = ListNode(digit)
node = node.next
return dummy.next
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. 修正いたしました! |
||
| node = dummy | ||
|
|
||
| total = carry = 0 | ||
|
|
||
| while l1 or l2 or carry: | ||
| total = carry | ||
|
|
||
| if l1: | ||
| total += l1.val | ||
| l1 = l1.next | ||
| if l2: | ||
| total += l2.val | ||
| l2 = l2.next | ||
|
|
||
| digit = total % 10 | ||
| carry = total // 10 | ||
| node.next = ListNode(digit) | ||
| node = node.next | ||
|
|
||
| return dummy.next | ||
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.
以下のコメントをご参照ください。
Kazuuuuuuu-u/arai60#2 (comment)
h1rosaka/arai60#47 (comment)
Uh oh!
There was an error while loading. Please reload this page.
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.
@nodchip
今回の場合だと、
にした方がより良いという認識で合っていますでしょうか?
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.
carry は int 型ですので、
が良いと思います。
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.
そうでした!ありがとうございます!!