diff --git a/2. Add Two Numbers.md b/2. Add Two Numbers.md new file mode 100644 index 0000000..5deda52 --- /dev/null +++ b/2. Add Two Numbers.md @@ -0,0 +1,158 @@ +# step1 +- 前回反省を踏まえてとりあえず紙に書きだしてみて全体の流れを抑えた +- l1とl2をループで回して加算をする +- 加算の結果10以上の場合は繰り上げが必要 + - 繰り上げフラグを立ててみる + - 繰り上げフラグが立っている場合は加算の結果に+1をする +- 検証が必要なこと + - 一時的にlistで計算結果を入れておいて、最後にListNodeに詰めていきたい。 + - ListNodeにどう詰めるか + - listに一時的に入れておく必要があるのか + +```python +# 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]: + l3 = list() + add_result = ListNode() + carry = 0 + while l1 is not None and l2 is not None: + tmp = l1.val+l2.val + if tmp >= 10: + l3.append(tmp % 10) + carry = 1 + elif carry == 1: + l3.append(tmp + 1) + else: + l3.append(tmp) + l1 = l1.next + l2 = l2.next + for i in l3: + # ここでListNodeに詰め込む処理を書きたい。 + return add_result +``` + +- l1=[9,9,9,9,9,9,9],l2=[9,9,9,9]のときに5ループ目で止まっている + - l1とl2が同じ長さのListNodeではないため。l2が先にNoneにwhileの条件で止まってしまっている。 + - whileの条件を or にして、l1とl2がNoneの場合に、l1とl2の加算で0を加算するような配慮が必要になりそう +- ListNodeの処理部分も思いつかなかったので答えや他の方のPRを見る + +# step2 +- コメント集結構量があったので理解できる範囲で読む。 + - https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.lxzt19oefrb8 + - +- https://github.com/hiro111208/leetcode/pull/5/changes + - ifの条件がis not Noneに見慣れているので個人的にはそっちの書き方が好み + - whileの中でListNodeに詰めていって、dummyをreturnするのでもいいのか + - //で商を繰り上げている +- https://github.com/rimokem/arai60/pull/5/changes + - if文が3項演算子 + - https://note.nkmk.me/python-if-conditional-expressions/ + - divmodという関数を使っている。割り算の商と余りを同時に取得できる + - 繰り上げを真偽値よりも数値で持つ方が3つ以上のリストの足し算にも対応できる +- https://github.com/Rinta-Rinta/LeetCode_arai60/pull/4/changes + - whileの条件で繰り上げがないことを含める必要がある + +# step3 +```python +# 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() + node = dummy + carry = 0 + while l1 is not None or l2 is not None or carry !=0: + if l1 is not None: + value1 = l1.val + else: + value1 = 0 + if l2 is not None: + value2 = l2.val + else: + value2 = 0 + tmp = value1 + value2 + carry + carry = tmp // 10 + digit = tmp % 10 + node.next = ListNode(digit) + node = node.next + if l1 is not None: + l1=l1.next + if l2 is not None: + l2=l2.next + return dummy.next +``` + +```python +# 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() + node = dummy + carry = 0 + while l1 is not None or l2 is not None or carry != 0: + if l1 is not None: + value1 = l1.val + else: + value1 = 0 + if l2 is not None: + value2 = l2.val + else: + value2 = 0 + tmp = value1 + value2 + carry + carry = tmp // 10 + digit = tmp % 10 + node.next = ListNode(digit) + node = node.next + if l1 is not None: + l1 = l1.next + if l2 is not None: + l2 = l2.next + return dummy.next +``` + +```python +# 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() + node = dummy + carry = 0 + while l1 is not None or l2 is not None or carry != 0: + if l1 is not None: + value1 = l1.val + else: + value1 = 0 + if l2 is not None: + value2 = l2.val + else: + value2 = 0 + tmp = value1 + value2 + carry + carry = tmp // 10 + digit = tmp % 10 + node.next = ListNode(digit) + node = node.next + if l1 is not None: + l1 = l1.next + if l2 is not None: + l2 = l2.next + return dummy.next +``` + +