Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions 2. Add Two Numbers/AddTwoNumbers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# step1 何も見ずに解く

## 解答
- 愚直に書いてみる
- 複数回に分けて`while`ループを回しているのを単純化したいが、パッと思いつかず
- 計算量
- 時間計算量: O(n)
- 入力のサイズは最大で100
- JITコンパイルされていなくても1ループ辺り多く見積もっても10μ秒程度あれば処理できそう
- 実行時間は2ms以下には収まりそう
- 空間計算量: O(1)
```java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
boolean isCarryUp = false;
// どちらのListNodeが長いか分からないので、
// とりあえずl1でループを回す
// このループ終了時点でl1は末尾まで到達
while (l1 != null) {
int l2Val = l2 != null ? l2.val : 0;
int sum = l1.val + l2Val + carryUp(isCarryUp);
isCarryUp = false;
if (sum > 9) {

Choose a reason for hiding this comment

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

些細な点ですが、マジックナンバーの理解しやすさという観点からsum > 9よりsum >= 10のほうが分かりやすいかもしれません。
Ryotaro25/leetcode_first60#5 (comment)
seal-azarashi/leetcode#5 (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.

処理内容を日本語で考えた場合、10以上と記述するのが自然ですね。ありがとうございます!

isCarryUp = true;
sum = sum - 10;
}
tail.next = new ListNode(sum);
tail = tail.next;
l1 = l1.next;
if (l2 != null) {
l2 = l2.next;
}
}

// l2の方が長かった場合は、
// 繰り上がりに留意してl2のノードを連結
while (l2 != null) {
int sum = l2.val + carryUp(isCarryUp);
isCarryUp = false;
if (sum > 9) {
isCarryUp = true;
sum = sum - 10;
}
tail.next = new ListNode(sum);
tail = tail.next;
l2 = l2.next;
}

// 最後のノードが繰り上がりの場合、末尾にノードを追加
if (isCarryUp) {
tail.next = new ListNode(1);
}
return dummy.next;
}

// 下位の桁で繰り上がりがあった場合は、1を返す
private int carryUp(Boolean isCarryUp) {
return isCarryUp ? 1 : 0;
}
}
```

# step2 他の方の解答を見る
- 参考
- https://github.com/fuga-98/arai60/pull/6/
- https://github.com/fhiyo/leetcode/pull/5
## 解答
- 単一の`while`で処理する実装
- intの変数`carry`に直接繰り上がりを足せば、フラグを立てる必要もないのか
```java
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode();
ListNode tail = dummy;
int carry = 0;
while (carry != 0 || l1 != null || l2 != null) {
if (l1 != null) {
carry += l1.val;
l1 = l1.next;
}
if (l2 != null) {
carry += l2.val;
l2 = l2.next;
}
tail.next = new ListNode(carry % 10);
tail = tail.next;
carry = carry / 10;
}
return dummy.next;
}
}
```
- 再帰を使った実装
- ついでに、nullチェックを関数化した実装も試してみる。
```java
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
return addTwoNumbers(l1, l2, 0);
}

private ListNode addTwoNumbers(ListNode l1, ListNode l2, int carry) {
if (carry == 0 && l1 == null && l2 == null) {
return null;
}

carry = carry + getValOrDefault(l1) + getValOrDefault(l2);
l1 = getNextOrNull(l1);
l2 = getNextOrNull(l2);
ListNode nextNode = addTwoNumbers(l1, l2, carry / 10);
return new ListNode(carry % 10, nextNode);
}

private int getValOrDefault(ListNode node) {
return node != null ? node.val : 0;
}
Comment on lines +125 to +127
Copy link

Choose a reason for hiding this comment

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

getNextOrNullもですが)
処理内容がメソッド名になっていて、メソッド名の長さと処理内容の長さがほぼ変わらないので個人的にメソッド化するほどでもないかなと思いました。

自分がこれくらい短い処理にメソッド名をつけるとしたら、処理内容の意図をメソッドに反映させたい時ですかね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

carry = carry + getValOrDefault(l1) + getValOrDefault(l2) ;
と書けるのがちょっと気持ちいいくらいの効果しか無いですね。

自分がこれくらい短い処理にメソッド名をつけるとしたら、処理内容の意図をメソッドに反映させたい時ですかね。

処理に名前を付けて意図を明示するということですね。その基準を意識してみます。


private ListNode getNextOrNull(ListNode node) {
return node == null ? null : node.next;
Copy link

Choose a reason for hiding this comment

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

このくらいの短さならいいと思うのですが、三項演算子はわかりにくいので、このコードはあまり好みじゃないです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます!
個人的にはワンライナーで書けるこの程度までは三項演算子で書いてしまいます。

とはいえ三項演算子を読みづらいという方も結構いることを心に留めておきます。
私もネストされた三項演算子とか読みづらいです。

}
}
```

# step3 3回ミスなく書く
- 単一の`while`を使った実装で書く
## 解答
```java
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode();
ListNode node = dummy;
Copy link

Choose a reason for hiding this comment

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

細かいですが、個人的にはdummyHeadなど何のダミーなのかが表現されている方がわかりやすく感じます。

int carry = 0;

while (carry != 0 || l1 != null || l2 != null) {
if (l1 != null) {
carry += l1.val;

Choose a reason for hiding this comment

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

carryにどんどん値が足されていくのは個人的には少し違和感を感じました。
変数は増えてしまいますがsumなどの変数を定義してそこに足していくほうが分かりやすく感じます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかにcarryという名前以上の責務を負わせてしまっていますね。
おっしゃる通りwhileループの外で繰り上がりを管理するcarry、ループの中で合計を管理するsumと分けて管理した方が丁寧ですね。

l1 = l1.next;
}
if (l2 != null) {
carry += l2.val;
l2 = l2.next;
}
node.next = new ListNode(carry % 10);
node = node.next;
carry = carry / 10;
}
return dummy.next;
}
}
```