Skip to content

Commit 1ea7649

Browse files
committed
feat: 0002.Add-Two-Numbers
1 parent 94c39bd commit 1ea7649

File tree

4 files changed

+227
-9
lines changed

4 files changed

+227
-9
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
---
2+
title: 0002.Add Two Numbers
3+
subtitle: "https://leetcode.com/problems/add-two-numbers/description/"
4+
date: 2024-03-02T15:57:00+08:00
5+
lastmod: 2024-03-02T15:57:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "0002.Add-Two-Numbers"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Medium, Add Two Numbers, Linked List, Math, Recursion, Amazon, Apple,Facebook,Microsoft,Bloomberg]
14+
categories: [LeetCode]
15+
16+
featuredImage: ""
17+
featuredImagePreview: ""
18+
19+
hiddenFromHomePage: false
20+
hiddenFromSearch: false
21+
twemoji: false
22+
lightgallery: true
23+
ruby: true
24+
fraction: true
25+
fontawesome: true
26+
linkToMarkdown: false
27+
rssFullText: false
28+
29+
toc:
30+
enable: true
31+
auto: true
32+
code:
33+
copy: true
34+
maxShownLines: 200
35+
math:
36+
enable: false
37+
# ...
38+
mapbox:
39+
# ...
40+
share:
41+
enable: true
42+
# ...
43+
comment:
44+
enable: true
45+
# ...
46+
library:
47+
css:
48+
# someCSS = "some.css"
49+
# located in "assets/"
50+
# Or
51+
# someCSS = "https://cdn.example.com/some.css"
52+
js:
53+
# someJS = "some.js"
54+
# located in "assets/"
55+
# Or
56+
# someJS = "https://cdn.example.com/some.js"
57+
seo:
58+
images: []
59+
# ...
60+
---
61+
# [0002.Add Two Numbers](https://leetcode.com/problems/add-two-numbers/description/)
62+
63+
## 題目
64+
65+
## 題目大意
66+
67+
68+
## 解題思路
69+
[鏈表雙指標技巧](https://labuladong.github.io/article/fname.html?fname=%E9%93%BE%E8%A1%A8%E6%8A%80%E5%B7%A7) 和加法運算過程中對進位的處理。 注意這個 carry 變數的處理,在我們手動類比加法過程的時候會經常用到。
70+
71+
* 遍歷 l1跟 l2. 講兩個list的val相加, 並且記錄進位的值給next使用
72+
* 最後如果 carry 還有的話, 需要產生一個新的節點
73+
74+
## Big O
75+
76+
* 時間複雜 : `O(max⁡(m,n)`
77+
時間複雜度: O(max⁡(m,n)) ,其中 m 和 n 分別為兩個鏈表的長度。 我們要遍歷兩個鏈表的全部位置,而處理每個位置只需要 O(1) 的時間
78+
79+
* 空間複雜 : `O(1)`
80+
O(1) 。 注意返回值不計入空間複雜度
81+
82+
## 來源
83+
* https://leetcode.com/problems/add-two-numbers/description/
84+
* https://leetcode.cn/problems/
85+
86+
## 解答
87+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0002.Add-Two-Numbers/main.go
88+
89+
```go
90+
package addtwonumbers
91+
92+
// 時間複雜 O(max(m,n)), 空間複雜 O(1)
93+
/**
94+
* Definition for singly-linked list.
95+
* type ListNode struct {
96+
* Val int
97+
* Next *ListNode
98+
* }
99+
*/
100+
101+
// 遍歷 l1跟 l2. 講兩個list的val相加, 並且記錄進位的值給next使用
102+
// 最後如果 carry 還有的話, 需要產生一個新的節點
103+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
104+
var result, tail *ListNode
105+
carry := 0
106+
for l1 != nil || l2 != nil {
107+
n1, n2 := 0, 0
108+
if l1 != nil {
109+
n1 = l1.Val
110+
l1 = l1.Next
111+
}
112+
if l2 != nil {
113+
n2 = l2.Val
114+
l2 = l2.Next
115+
}
116+
sum := n1 + n2 + carry
117+
sum, carry = sum%10, sum/10
118+
119+
if result == nil {
120+
result = &ListNode{Val: sum, Next: nil}
121+
tail = result
122+
} else {
123+
tail.Next = &ListNode{Val: sum, Next: nil}
124+
tail = tail.Next
125+
}
126+
}
127+
// 最後如果 carry 還有的話, 需要產生一個新的節點
128+
if carry > 0 {
129+
tail.Next = &ListNode{Val: carry, Next: nil}
130+
}
131+
return result
132+
}
133+
```
134+
135+
## Benchmark
136+
137+
```sh
138+
139+
```

Leetcode/0002.Add-Two-Numbers/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package addtwonumbers
2+
3+
// 時間複雜 O(max(m,n)), 空間複雜 O(1)
4+
/**
5+
* Definition for singly-linked list.
6+
* type ListNode struct {
7+
* Val int
8+
* Next *ListNode
9+
* }
10+
*/
11+
12+
// 遍歷 l1跟 l2. 講兩個list的val相加, 並且記錄進位的值給next使用
13+
// 最後如果 carry 還有的話, 需要產生一個新的節點
14+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
15+
var result, tail *ListNode
16+
carry := 0
17+
for l1 != nil || l2 != nil {
18+
n1, n2 := 0, 0
19+
if l1 != nil {
20+
n1 = l1.Val
21+
l1 = l1.Next
22+
}
23+
if l2 != nil {
24+
n2 = l2.Val
25+
l2 = l2.Next
26+
}
27+
sum := n1 + n2 + carry
28+
sum, carry = sum%10, sum/10
29+
30+
if result == nil {
31+
result = &ListNode{Val: sum, Next: nil}
32+
tail = result
33+
} else {
34+
tail.Next = &ListNode{Val: sum, Next: nil}
35+
tail = tail.Next
36+
}
37+
}
38+
// 最後如果 carry 還有的話, 需要產生一個新的節點
39+
if carry > 0 {
40+
tail.Next = &ListNode{Val: carry, Next: nil}
41+
}
42+
return result
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// TODO : 0002.Add-Two-Numbers Unit Test
2+
package
3+
4+
import "testing"
5+
6+
var tests = []struct {
7+
arg1 string
8+
want int
9+
}{
10+
{
11+
"bbbab",
12+
4,
13+
},
14+
}
15+
16+
func TestLongestPalindromeSubseq(t *testing.T) {
17+
for _, tt := range tests {
18+
// if got := ReverseList(tt.arg1); !reflect.DeepEqual(got, tt.want) {
19+
if got := LongestPalindromeSubseq(tt.arg1); got != tt.want {
20+
t.Errorf("got = %v, want = %v", got, tt.want)
21+
}
22+
}
23+
}
24+
25+
func BenchmarkLongestPalindromeSubseq(b *testing.B) {
26+
b.ResetTimer()
27+
for i := 0; i < b.N; i++ {
28+
LongestPalindromeSubseq(tests[0].arg1)
29+
}
30+
}
31+
32+
/*
33+
go test -benchmem -run=none LeetcodeGolang/Leetcode/0354.Russian-Doll-Envelopes -bench=.
34+
35+
*/

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,16 @@ https://kimi0230.github.io/LeetcodeGolang/
8484
---
8585
#### Linked List
8686

87-
| No. | Title | Solution | Difficulty | Time | Space | Topic |
88-
|---------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------------------:|------------|----------|-------|---------------------------|
89-
| [0019](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0019.Remove-Nth-Node-From-End-of-List/) | [Remove Nth Node From End of List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0019.Remove-Nth-Node-From-End-of-List) | Medium | O(n) | O(1) | Linked List, Two Pointers |
90-
| [0141](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0141.Linked-List-Cycle/) | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0141.Linked-List-Cycle) | Easy | O(n) | O(1) | Linked List, Two Pointers |
91-
| [0142](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0142.Linked-List-CycleII/) | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0142.Linked-List-CycleII) | Medium | O(n) | O(1) | Linked List, Two Pointers |
92-
| [0203](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0203.Remove-Linked-List-Elements) | [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0203.Remove-Linked-List-Elements) | Easy | O(n) | O(1) | Linked List |
93-
| [0206](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0206.Reverse-Linked-List) | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Go](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0206.Reverse-Linked-List) | Easy | O(n) | O(1) | Linked List |
94-
| [0876](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0876.Middle-of-the-Linked-List/) | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0876.Middle-of-the-Linked-List) | Easy | | | Linked List, Two Pointers |
95-
| [0021](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0021.Merge-Two-Sorted-Lists/) | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0021.Merge-Two-Sorted-Lists) | Easy | O(log n) | O(1) | Linked List |
87+
| No. | Title | Solution | Difficulty | Time | Space | Topic |
88+
|---------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------------------:|------------|-------------|-------|---------------------------|
89+
| [0019](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0019.Remove-Nth-Node-From-End-of-List/) | [Remove Nth Node From End of List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0019.Remove-Nth-Node-From-End-of-List) | Medium | O(n) | O(1) | Linked List, Two Pointers |
90+
| [0141](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0141.Linked-List-Cycle/) | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0141.Linked-List-Cycle) | Easy | O(n) | O(1) | Linked List, Two Pointers |
91+
| [0142](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0142.Linked-List-CycleII/) | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0142.Linked-List-CycleII) | Medium | O(n) | O(1) | Linked List, Two Pointers |
92+
| [0203](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0203.Remove-Linked-List-Elements) | [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0203.Remove-Linked-List-Elements) | Easy | O(n) | O(1) | Linked List |
93+
| [0206](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0206.Reverse-Linked-List) | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [Go](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0206.Reverse-Linked-List) | Easy | O(n) | O(1) | Linked List |
94+
| [0876](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0876.Middle-of-the-Linked-List/) | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0876.Middle-of-the-Linked-List) | Easy | | | Linked List, Two Pointers |
95+
| [0021](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0021.Merge-Two-Sorted-Lists/) | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0021.Merge-Two-Sorted-Lists) | Easy | O(log n) | O(1) | Linked List |
96+
| [0002](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0002.Add-Two-Numbers/) | [Add Two Number](https://leetcode.com/problems/add-two-numbers//) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0002.Add-Two-Numbers) | Medium | O(max(m,n)) | O(1) | Linked List |
9697

9798
---
9899

0 commit comments

Comments
 (0)