From e7755dbd4e18d70e850615ceaaa194b6545ad27e Mon Sep 17 00:00:00 2001 From: soma <140805585+Exzrgs@users.noreply.github.com> Date: Sat, 19 Oct 2024 23:17:11 +0900 Subject: [PATCH] Revert "Add Two Numbers" --- arai60/add-two-numbers/step1.py | 33 -------------------- arai60/add-two-numbers/step2.py | 53 --------------------------------- arai60/add-two-numbers/step3.py | 27 ----------------- arai60/add-two-numbers/step4.py | 24 --------------- 4 files changed, 137 deletions(-) delete mode 100644 arai60/add-two-numbers/step1.py delete mode 100644 arai60/add-two-numbers/step2.py delete mode 100644 arai60/add-two-numbers/step3.py delete mode 100644 arai60/add-two-numbers/step4.py diff --git a/arai60/add-two-numbers/step1.py b/arai60/add-two-numbers/step1.py deleted file mode 100644 index b643a96..0000000 --- a/arai60/add-two-numbers/step1.py +++ /dev/null @@ -1,33 +0,0 @@ -''' -nextを作る位置を工夫しないと最後に余計なノードを作ってしまう -最後にc==1で繰り上がりを足さないといけないことに最初は気づいていたが、実装しているうちに忘れてしまっていた... -''' - -class Solution: - def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: - head = ListNode(-1) - now = head - - c = 0 - while l1 and l2: - s = (l1.val + l2.val + c)%10 - c = (l1.val + l2.val + c)//10 - now.next = ListNode(s) - now = now.next - l1 = l1.next - l2 = l2.next - while l1: - s = (l1.val + c)%10 - c = (l1.val + c)//10 - now.next = ListNode(s) - now = now.next - l1 = l1.next - while l2: - s = (l2.val + c)%10 - c = (l2.val + c)//10 - now.next = ListNode(s) - now = now.next - l2 = l2.next - if c == 1: - now.next = ListNode(1) - return head.next diff --git a/arai60/add-two-numbers/step2.py b/arai60/add-two-numbers/step2.py deleted file mode 100644 index 4cda706..0000000 --- a/arai60/add-two-numbers/step2.py +++ /dev/null @@ -1,53 +0,0 @@ -''' -headという変数名は答えのheadを表していそうだから、fake_headに変える -nowもtailに変える -whileが多いからまとめる。 - ただ、実際1つのループにまとめるのが困難なケースもある気がするので、そこは実装する前に考えたほうが良いかもしれない - -口頭説明シミュレーション - まず、fake_headを作ります。 - あとから後ろからノードをたどっていくのは大変なので、素早くheadを指せるポインタが欲しいからです。 - 次に、現在処理しているノードを表すtailを用意します。 - また、l1とl2を足したときに繰り上がりが発生する可能性があるので、carryもここで用意しておきます。 - l1かl2がNoneでないか、carryが1の間ループをします。 - carryを入れる理由は、最後に繰り上がりだけ残った場合をループの中で処理したいからです。 - そしてsum_valという変数は、それらを足した値とします。 - carryを桁あふれ、つまりsum_valを10で割った商とします。 - また、sum_valの10で割ったあまりをtailの次のノードの値とします。 - そして、tail, l1, l2を次のステップへと進めます。 - こうすることで、問題を解くことができました。 - 最後に桁上りしてl1とl2が終了する場合や、空のL1,l2を渡された場合にも対処できていることも確認できます。 - - Firstly, we create "fake_head". - This is because we want a pointer that can quickly point to the head. - Next, we prepare "tail" that represents the node currently being processed. - We also need to prepare "carry" because there is possibility of carry-over when l1 and l2 added. - We loop while l1 or l2 is not None or if carry is 1. - The reason for including "carry" is that we want to handle the case where only the carry remains in the loop. - The variable "sum_val" is the value obtained by adding them together. - Let carry be the overflow. In other words, it is the quotient of sum_val divided by 10. - And let sum_val's remainder divided by 10 be the value of the next node of tail. - Then we proceed to the next step for tail, l1, and l2. - In this way, we have solved the problem. - We can also confirm that we are able to deal with the case where l1 and l2 terminate after overflow or when an empty l1 and l2 are passed. -''' - -class Solution: - def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: - fake_head = ListNode(-1) - tail = fake_head - - carry = 0 - while l1 or l2 or carry: - sum_val = carry - if l1: - sum_val += l1.val - l1 = l1.next - if l2: - sum_val += l2.val - l2 = l2.next - carry = sum_val//10 - tail.next = ListNode(sum_val%10) - tail = tail.next - - return fake_head.next diff --git a/arai60/add-two-numbers/step3.py b/arai60/add-two-numbers/step3.py deleted file mode 100644 index c671a47..0000000 --- a/arai60/add-two-numbers/step3.py +++ /dev/null @@ -1,27 +0,0 @@ -''' -意識したこと -・fake_headを作る必要性 -確認したこと -・各ポインタをちゃんと次のステップへ移動させているか? -・l1やl2が空のときにしっかりと動くか? -''' - -class Solution: - def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: - fake_head = ListNode(-1) - tail = fake_head - - carry = 0 - while l1 or l2 or carry: - sum_val = carry - if l1: - sum_val += l1.val - l1 = l1.next - if l2: - sum_val += l2.val - l2 = l2.next - carry = sum_val//10 - tail.next = ListNode(sum_val%10) - tail = tail.next - - return fake_head.next diff --git a/arai60/add-two-numbers/step4.py b/arai60/add-two-numbers/step4.py deleted file mode 100644 index f41ed86..0000000 --- a/arai60/add-two-numbers/step4.py +++ /dev/null @@ -1,24 +0,0 @@ -''' -変数名をfake_head→dummy -%の両端にspace -''' - -class Solution: - def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: - dummy = ListNode(-1) - tail = dummy - - carry = 0 - while l1 or l2 or carry: - sum_val = carry - if l1: - sum_val += l1.val - l1 = l1.next - if l2: - sum_val += l2.val - l2 = l2.next - carry = sum_val // 10 - tail.next = ListNode(sum_val % 10) - tail = tail.next - - return dummy.next