Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

2. Add Two Numbers #7

wants to merge 1 commit into from

Conversation

rinost081
Copy link
Owner

@rinost081 rinost081 commented Sep 8, 2024

  1. Add Two Numbers を解きました。
    問題: https://leetcode.com/problems/add-two-numbers/
    言語: Python
    レビューをお願いします。

Copy link

@Yoshiki-Iwasa Yoshiki-Iwasa left a 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

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)

Choose a reason for hiding this comment

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

answerという変数名を使うことは避けたほうが良いと思います

初めてこのコードを読む人からすると、「この変数が一体何か」が関数の最後まで読まないとわかりません。
関数の先頭から読んでいって、理屈と意図がわかるようなコードを書くのが理想的かなと思います

この場合、sentinel, dummyなどが適切でしょうか

参考: hroc135/leetcode#15 (comment)

Copy link
Owner Author

Choose a reason for hiding this comment

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

LeetCodeのような問題に対する回答として、answerとつけてましたがその通りですね。ありがとうございます

Copy link

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

Choose a reason for hiding this comment

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

同様に、ここはnodeなどで十分かなと。

Comment on lines +55 to +61
answer.val = l1_val + l2_val + carry

if answer.val > 9:
answer.val = answer.val % 10
carry = 1
else:
carry = 0

Choose a reason for hiding this comment

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

Suggested change
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

でまとめられそうです。

Comment on lines +49 to +50
answer.next = ListNode()
answer = answer.next

Choose a reason for hiding this comment

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

計算結果から新しいnodeを作成 -> 現在のお尻につけるのほうが自然じゃないでしょうか?

@Yoshiki-Iwasa
Copy link

Step2以降に取り組むとき、他の方の解法やコメントを検索して見てみることをおすすめします

answer_head = answer
carry = 0

while l1 or l2:

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:
Copy link

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:
Copy link

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:
Copy link

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)
Copy link

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)
Copy link

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:

Choose a reason for hiding this comment

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

carry = carry / 10などするとこの分岐を消せそうですね。

@liquo-rice
Copy link

PRのタイトルですが、"2. Add Two Numbers"のようにしたほうが後で見つけやすいかもしれません。

@rinost081 rinost081 changed the title LeetCode 2 2. Add Two Numbers Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants