Conversation
24_2.py
Outdated
| #3rd:4min | ||
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| dummyHead = ListNode(0) |
There was a problem hiding this comment.
Pythonで用いる一般的な命令規則で_を使うというのがあるので、dummy_headの方が良い気がします。(LeetCodeの仕様で決まる名前は別として)
There was a problem hiding this comment.
| tail.next = next_node | ||
| tail = tail.next | ||
|
|
||
| l1 = l1.next if l1 else None |
There was a problem hiding this comment.
空の場合はl1.next = Noneになっていると思うので、l1 = l1.nextで良さそうです。
24_2.py
Outdated
| l1 = l1.next if l1 else None | ||
| l2 = l2.next if l2 else None | ||
|
|
||
| l3 = dummyHead.next |
There was a problem hiding this comment.
好みかもしれませんが、return dummyHead.nextの方が分りやすいかも?l3だとl1,l2と関連がありそうな気がしてしまいます。
There was a problem hiding this comment.
dummy を使わない書き方もあるので見ておいてください。
https://discord.com/channels/1084280443945353267/1226508154833993788/1246022270984392724
There was a problem hiding this comment.
確かにそのまま返す内容と変わらないのに変数にすることもないと思うので、そのままreturnしようと思います。l1やl2から生成されるl3というニュアンスで書いていましたが、他の方からも分かりにくいという指摘があるので、変数名を考える場合はもう少し意味のある名前にしようと思います。ありがとうございます。
There was a problem hiding this comment.
dummyの解法を見てからそれ以外の解法が頭になかったように思います。シンプルにifの場合分けでdummyを使わない書き方もできますね。ありがとうございます。
24_2.py
Outdated
| #3rd:4min | ||
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| dummyHead = ListNode(0) |
There was a problem hiding this comment.
valは使わない、かつ、デフォルトと同じなので、ListNode()でいいと思います。
There was a problem hiding this comment.
仰るように、わざわざ0と明示する必要もないですね。修正させていただきます。
24_2.py
Outdated
| tail = dummyHead | ||
| carry = 0 | ||
|
|
||
| while l1 != None or l2 != None or carry != 0: |
There was a problem hiding this comment.
isは〇〇かどうか、 ==は評価、というイメージでなんとなく使っておりました。
調べてみると、isは同一objか判定しており、==は同じ値かを判別している。ということで、
特に、Noneオブジェクトとの比較の際にはisが推奨されているということは知りませんでした。
| carry = 0 | ||
|
|
||
| while l1 != None or l2 != None or carry != 0: | ||
| l1_val = l1.val if l1 else 0 |
There was a problem hiding this comment.
if l1とl1 != Noneの二つの書き方が混在しているので、どちらかに統一した方がいいです。
There was a problem hiding this comment.
今まで意識せず書いておりました。修正しようと思います。
24_2.py
Outdated
| def add_two_num_helper(l1, l2, carry, l3): | ||
| if l1 is None and l2 is None and carry == 0: | ||
| return None | ||
| else: |
24_2.py
Outdated
| l3.next = add_two_num_helper(l1, l2, carry, l3) | ||
| return l3 | ||
| l3 = add_two_num_helper(l1, l2, carry, l3) | ||
| result = dummyHead.next |
There was a problem hiding this comment.
torotokiさんのコメントにもありましたが、わざわざ新しい変数を作る必要はないかと思います。上のnext_valも同様です。
There was a problem hiding this comment.
仰るように一度しか出現せず、そのような変数は修正しようと思います。ありがとうございます。
24_2.py
Outdated
| next_node = ListNode(next_val) | ||
| l3.next = next_node | ||
| l3 = l3.next | ||
| l1 = l1.next if l1 else None |
There was a problem hiding this comment.
上記でif l1確認をしているものの、
- l1 がNone かつ l2がNone を確認
- l1やl2がNoneの場合に0を変数に入れている
確認をしているのみなので、l1 がNone もしくは l2がNone のケースで、l1 = l1.next if l1 else Noneをl1 = l1.nextとするとNone.nextとなるケースが発生して、エラーが出てしまうのではないかなと思いました。
There was a problem hiding this comment.
コメントいただいた意味を理解できておりませんでした。
確かに再度コメントいただいたようにも書けますね。補足をありがとうございます。
24_2.py
Outdated
| l3 = l3.next | ||
| l1 = l1.next if l1 else None | ||
| l2 = l2.next if l2 else None | ||
| l3.next = add_two_num_helper(l1, l2, carry, l3) |
There was a problem hiding this comment.
末尾再帰となるように修正したコードを後ほどcommitしようと思います。これまで末尾再帰を聞いたことしかなく意識したことがなかったので、違う点などあればご指摘いただけると嬉しいです。
24_2.py
Outdated
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| dummyHead = ListNode(0) | ||
| l3 = dummyHead |
There was a problem hiding this comment.
l3に意味のある名前をつけた方がいいかと思います。特にadd_two_num_helper内では理解しにくくなります。
There was a problem hiding this comment.
ありがとうございます。変数名をつける際に、同じようなものくらいのニュアンスしかなかったと思いました。変数名を考えようと思います。
24_2.py
Outdated
| #再帰を使う方法 | ||
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| dummyHead = ListNode(0) |
There was a problem hiding this comment.
仰るようにdummyHeadが無くても問題ないですね。そのように修正してみます。
su33331
left a comment
There was a problem hiding this comment.
コメントいただいた方、ありがとうございます。
後ほど修正版をcommitさせていただきます。
24_2.py
Outdated
| l1 = l1.next if l1 else None | ||
| l2 = l2.next if l2 else None | ||
|
|
||
| l3 = dummyHead.next |
There was a problem hiding this comment.
確かにそのまま返す内容と変わらないのに変数にすることもないと思うので、そのままreturnしようと思います。l1やl2から生成されるl3というニュアンスで書いていましたが、他の方からも分かりにくいという指摘があるので、変数名を考える場合はもう少し意味のある名前にしようと思います。ありがとうございます。
24_2.py
Outdated
| #3rd:4min | ||
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| dummyHead = ListNode(0) |
There was a problem hiding this comment.
仰るように、わざわざ0と明示する必要もないですね。修正させていただきます。
24_2.py
Outdated
| #再帰を使う方法 | ||
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| dummyHead = ListNode(0) |
There was a problem hiding this comment.
仰るようにdummyHeadが無くても問題ないですね。そのように修正してみます。
24_2.py
Outdated
| next_node = ListNode(next_val) | ||
| l3.next = next_node | ||
| l3 = l3.next | ||
| l1 = l1.next if l1 else None |
There was a problem hiding this comment.
上記でif l1確認をしているものの、
- l1 がNone かつ l2がNone を確認
- l1やl2がNoneの場合に0を変数に入れている
確認をしているのみなので、l1 がNone もしくは l2がNone のケースで、l1 = l1.next if l1 else Noneをl1 = l1.nextとするとNone.nextとなるケースが発生して、エラーが出てしまうのではないかなと思いました。
24_2.py
Outdated
| l3 = l3.next | ||
| l1 = l1.next if l1 else None | ||
| l2 = l2.next if l2 else None | ||
| l3.next = add_two_num_helper(l1, l2, carry, l3) |
There was a problem hiding this comment.
末尾再帰となるように修正したコードを後ほどcommitしようと思います。これまで末尾再帰を聞いたことしかなく意識したことがなかったので、違う点などあればご指摘いただけると嬉しいです。
24_2.py
Outdated
| l3.next = add_two_num_helper(l1, l2, carry, l3) | ||
| return l3 | ||
| l3 = add_two_num_helper(l1, l2, carry, l3) | ||
| result = dummyHead.next |
There was a problem hiding this comment.
仰るように一度しか出現せず、そのような変数は修正しようと思います。ありがとうございます。
24_2.py
Outdated
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| dummyHead = ListNode(0) | ||
| l3 = dummyHead |
There was a problem hiding this comment.
ありがとうございます。変数名をつける際に、同じようなものくらいのニュアンスしかなかったと思いました。変数名を考えようと思います。
| carry = 0 | ||
|
|
||
| while l1 != None or l2 != None or carry != 0: | ||
| l1_val = l1.val if l1 else 0 |
There was a problem hiding this comment.
今まで意識せず書いておりました。修正しようと思います。
24_2.py
Outdated
| tail = dummyHead | ||
| carry = 0 | ||
|
|
||
| while l1 != None or l2 != None or carry != 0: |
There was a problem hiding this comment.
isは〇〇かどうか、 ==は評価、というイメージでなんとなく使っておりました。
調べてみると、isは同一objか判定しており、==は同じ値かを判別している。ということで、
特に、Noneオブジェクトとの比較の際にはisが推奨されているということは知りませんでした。
24_2.py
Outdated
| l1 = l1.next if l1 else None | ||
| l2 = l2.next if l2 else None | ||
|
|
||
| l3 = dummyHead.next |
There was a problem hiding this comment.
dummyの解法を見てからそれ以外の解法が頭になかったように思います。シンプルにifの場合分けでdummyを使わない書き方もできますね。ありがとうございます。
|
書いたコードを上書き修正せず、新しく付け足すことが推奨されています。https://discord.com/channels/1084280443945353267/1235066663150420038/1237295008541966336 |
https://leetcode.com/problems/add-two-numbers/description/?envType=problem-list-v2&envId=ryq3bf2c