Open
Conversation
|
読みやすくて良かったです! |
katsukii
reviewed
Feb 6, 2025
Comment on lines
+133
to
+134
| digit = total % 10 | ||
| carry = total // 10 |
There was a problem hiding this comment.
このように書く方法もあるようです。
carry, digit = divmod(total, 10)
これがいいとというより、選択肢の一つとしてご紹介です。
Owner
Author
There was a problem hiding this comment.
コメントありがとうございます。
その組み込み関数は知りませんでした。勉強になります。
使いどころが限定されてそうですが今回の問題にはピッタリですね。
oda
reviewed
Feb 6, 2025
|
|
||
| while carry != 0 or l1 is not None or l2 is not None: | ||
| total = carry | ||
|
|
There was a problem hiding this comment.
if not l1:
l1 = ListNode(0)
if not l2:
l2 = ListNode(0)という番兵を使う手があります。
Owner
Author
There was a problem hiding this comment.
コメントありがとうございます。
Noneの時は0埋めするということですね。実装してみました。
total = l1.val + l2.val + carryと書けるのが良いです。
新たにListNodeを追加してしまいますが、どのような使われ方をするか見て問題なさそうであれば変なリスクは取らずに可読性を優先するということですね。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題:https://leetcode.com/problems/add-two-numbers/description/