Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions 2. Add Two Numbers.md
Original file line number Diff line number Diff line change
@@ -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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

+ の両側にスペースが空けるのと空いていないのが統一されていない点が気になりました。空けるほうに統一するとよいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます、自分もあけてある方が読みやすいのでそちらで統一します

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
Comment on lines +138 to +145
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的にはこっちの方が2行で済んで読みやすいかと思いました。

value1 = l1.val if l1 is not None else 0
value2 = l2.val if l2 is not None else 0

tmp = value1 + value2 + carry
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここの変数名は例えばdigit_sumとするとtmpよりもわかりやすいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます、tmpは何が入っているか分かりにくいですね

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
Comment on lines +151 to +154
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここに関しても同様です。

l1 = l1.next if l1 else None
l2 = l2.next if l2 else None

return dummy.next
```