-
Notifications
You must be signed in to change notification settings - Fork 0
2. Add Two Numbers #4
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
su33331
wants to merge
2
commits into
main
Choose a base branch
from
su_24_2_2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| ## 2. Add Two Numbers | ||
| ### 24問目 | ||
| ### [問題リンク](https://leetcode.com/problems/add-two-numbers/description/?envType=problem-list-v2&envId=ryq3bf2c) | ||
|
|
||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, val=0, next=None): | ||
| # self.val = val | ||
| # self.next = next | ||
|
|
||
| #1st:書ききれずに解答を見る | ||
| #2nd:13min | ||
| #3rd:4min | ||
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| dummy_head = ListNode() | ||
| tail = dummy_head | ||
| carry = 0 | ||
|
|
||
| while l1 or l2 or carry != 0: | ||
| l1_val = l1.val if l1 else 0 | ||
| l2_val = l2.val if l2 else 0 | ||
| total = l1_val + l2_val + carry | ||
| carry = total // 10 | ||
|
|
||
| next_node = ListNode(total % 10) | ||
| tail.next = next_node | ||
| tail = tail.next | ||
|
|
||
| l1 = l1.next if l1 else None | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 空の場合はl1.next = Noneになっていると思うので、l1 = l1.nextで良さそうです。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あれ、それは None.next アクセスした途端に例外では? |
||
| l2 = l2.next if l2 else None | ||
|
|
||
| return dummy_head.next | ||
|
|
||
| ### 参考を見て書いた他方法 | ||
| #再帰を使う方法 | ||
| #dummy_headを使わない方法 | ||
| class Solution: | ||
| def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
| carry = 0 | ||
| def add_two_num_helper(l1, l2, carry): | ||
| if l1 is None and l2 is None and carry == 0: | ||
| return None | ||
|
|
||
| l1_val = l1.val if l1 else 0 | ||
| l2_val = l2.val if l2 else 0 | ||
| total = l1_val + l2_val + carry | ||
| carry = total // 10 | ||
|
|
||
| new_node = ListNode(total % 10) | ||
| sum_node_head = new_node | ||
| l1 = l1.next if l1 else None | ||
| l2 = l2.next if l2 else None | ||
| sum_node_head.next = add_two_num_helper(l1, l2, carry) | ||
| return sum_node_head | ||
| return add_two_num_helper(l1, l2, carry) | ||
|
|
||
| ''' | ||
| 参考にしたもの | ||
| * https://github.com/cheeseNA/leetcode/pull/2 | ||
| → 再帰を使った方法 | ||
| → python3のintはdoubleに相当し、その上限はメモリに依存する → | ||
| [python docのint概要](https://docs.python.org/ja/3/library/functions.html#int) | ||
| → int()の第二引数で基数を指定することで、異なる基数の値も整数値への変換もできる。文字列も可能。 | ||
| → 逆に2,8,16進数への変換は bin(), oct(), hex() | ||
| ''' | ||
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.
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.
if l1とl1 != Noneの二つの書き方が混在しているので、どちらかに統一した方がいいです。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.
今まで意識せず書いておりました。修正しようと思います。