-
Notifications
You must be signed in to change notification settings - Fork 0
2. Add Two Numbers #7
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
base: main
Are you sure you want to change the base?
Conversation
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.
見ました
class Solution: | ||
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
answer = l1 | ||
flag = False |
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.
Step1で解くときでもflag
のような情報量がない変数名は避けたほうが良いかなと思います
自分で読んでて認知負荷が高くなるので、バグの温床になるかと
""" | ||
class Solution: | ||
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
answer = ListNode(-1) |
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.
answer
という変数名を使うことは避けたほうが良いと思います
初めてこのコードを読む人からすると、「この変数が一体何か」が関数の最後まで読まないとわかりません。
関数の先頭から読んでいって、理屈と意図がわかるようなコードを書くのが理想的かなと思います
この場合、sentinel
, dummy
などが適切でしょうか
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.
LeetCodeのような問題に対する回答として、answerとつけてましたがその通りですね。ありがとうございます
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.
answer
というからには return answer
を期待するがそうではない、というのが抵抗感の一番の要因だと思っています。
意味が「答え」なのだからこれ自体が「足した答え」になると期待しますが、裏切られます。
class Solution: | ||
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
answer = ListNode(-1) | ||
answer_head = answer |
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.
同様に、ここはnode
などで十分かなと。
answer.val = l1_val + l2_val + carry | ||
|
||
if answer.val > 9: | ||
answer.val = answer.val % 10 | ||
carry = 1 | ||
else: | ||
carry = 0 |
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.
answer.val = l1_val + l2_val + carry | |
if answer.val > 9: | |
answer.val = answer.val % 10 | |
carry = 1 | |
else: | |
carry = 0 | |
sum = l1_val + l2_val + carry | |
answer.val = sum % 10 | |
carry = sum // 10 |
でまとめられそうです。
answer.next = ListNode() | ||
answer = answer.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.
計算結果から新しいnodeを作成 -> 現在のお尻につけるのほうが自然じゃないでしょうか?
Step2以降に取り組むとき、他の方の解法やコメントを検索して見てみることをおすすめします |
answer_head = answer | ||
carry = 0 | ||
|
||
while l1 or l2: |
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の条件も入れれば最後のif carry
を消せそうです
|
||
|
||
while l1 and l2: | ||
if flag: |
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.
インデントのスペースの数を統一することをお勧めいたします。
https://peps.python.org/pep-0008/#indentation
Use 4 spaces per indentation level.
https://google.github.io/styleguide/pyguide.html#34-indentation
Indent your code blocks with 4 spaces.
|
||
|
||
while l1 and l2: | ||
if flag: |
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.
l1.val += l2.val
if flag:
l1.val += 1
と書いたほうがシンプルだと思います。
else: | ||
l1.val = l1.val + l2.val | ||
|
||
if l1.val > 9: |
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.
flag = l1.val > 9
l1.val %= 10
と書いたほうがシンプルだと思います。
"""step2 | ||
これまでみたいに長々と書かずに解けるものなのだと思ってstep1は途中で辞めたんですけど、別の関数を作ったり結構長めなコードを書いている人を見たので変な先入観は捨てます、、、 | ||
step1でやりたかった処理は以下の通り | ||
時間計算量は0(n) |
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.
0(n) → O(n)
""" | ||
class Solution: | ||
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
answer = ListNode(-1) |
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.
dummy_head や sentinel という変数名を使われている方もいらっしゃいます。他の書かれたソースコードを読んで参考にしてみてください。
|
||
answer.val = l1_val + l2_val + carry | ||
|
||
if answer.val > 9: |
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 = carry / 10
などするとこの分岐を消せそうですね。
PRのタイトルですが、"2. Add Two Numbers"のようにしたほうが後で見つけやすいかもしれません。 |
問題: https://leetcode.com/problems/add-two-numbers/
言語: Python
レビューをお願いします。