Skip to content

Solve 2. Add Two Numbers#2

Open
cheeseNA wants to merge 3 commits intomainfrom
2
Open

Solve 2. Add Two Numbers#2
cheeseNA wants to merge 3 commits intomainfrom
2

Conversation

@cheeseNA
Copy link
Copy Markdown
Owner

def listNodeToNumber(self, l: ListNode) -> int:
node = l
power = 1
number = 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Python で int に代入することのできる値の範囲はいくつからいくつまでですか?また、ほかの主要な言語ではどうですか?

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.

コメントありがとうございます!
Pythonではintにメモリが許す分大きな値をいれることができます. そのため, intに代入することのできる値の範囲はメモリの大きさや処理系(CPythonかどうか, 64bit platformか)に依存します. 今回はlinked listの長さがたかだか100だったから良かったものの, 長さが大きくなるとそういったところも考えなければいけなくなるのですね.

また, 他の主要な言語では可変長整数は一般的ではなく, 扱える値の範囲は決まっております.
このold.pyの解答はpythonだからこそ簡単そうに実装できたということで, もし高速化のためc++で実装しようとなったときにはこの方針は使えませんね.
大変勉強になりました.

carry = 0
while True:
digit_sum = None
if l1 is None and l2 is None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

while 文をシンプルにするため、 l1 is None and l2 is None を while 文の条件式にし、 carry の処理を while 文の外で行ったほうがよいと思いました。

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.

なるほど, たしかにおっしゃるとおりですね…
とりあえずwhileをwhile Trueで始めてしまうクセがあるので気をつけます.
ありがとうございます.

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.

2 participants