Skip to content

Commit

Permalink
feat: 0002.Add-Two-Numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
kimi0230 committed Mar 2, 2024
1 parent 94c39bd commit 1ea7649
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 9 deletions.
139 changes: 139 additions & 0 deletions Leetcode/0002.Add-Two-Numbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
---
title: 0002.Add Two Numbers
subtitle: "https://leetcode.com/problems/add-two-numbers/description/"
date: 2024-03-02T15:57:00+08:00
lastmod: 2024-03-02T15:57:00+08:00
draft: false
author: "Kimi.Tsai"
authorLink: "https://kimi0230.github.io/"
description: "0002.Add-Two-Numbers"
license: ""
images: []

tags: [LeetCode, Go, Medium, Add Two Numbers, Linked List, Math, Recursion, Amazon, Apple,Facebook,Microsoft,Bloomberg]
categories: [LeetCode]

featuredImage: ""
featuredImagePreview: ""

hiddenFromHomePage: false
hiddenFromSearch: false
twemoji: false
lightgallery: true
ruby: true
fraction: true
fontawesome: true
linkToMarkdown: false
rssFullText: false

toc:
enable: true
auto: true
code:
copy: true
maxShownLines: 200
math:
enable: false
# ...
mapbox:
# ...
share:
enable: true
# ...
comment:
enable: true
# ...
library:
css:
# someCSS = "some.css"
# located in "assets/"
# Or
# someCSS = "https://cdn.example.com/some.css"
js:
# someJS = "some.js"
# located in "assets/"
# Or
# someJS = "https://cdn.example.com/some.js"
seo:
images: []
# ...
---
# [0002.Add Two Numbers](https://leetcode.com/problems/add-two-numbers/description/)

## 題目

## 題目大意


## 解題思路
[鏈表雙指標技巧](https://labuladong.github.io/article/fname.html?fname=%E9%93%BE%E8%A1%A8%E6%8A%80%E5%B7%A7) 和加法運算過程中對進位的處理。 注意這個 carry 變數的處理,在我們手動類比加法過程的時候會經常用到。

* 遍歷 l1跟 l2. 講兩個list的val相加, 並且記錄進位的值給next使用
* 最後如果 carry 還有的話, 需要產生一個新的節點

## Big O

* 時間複雜 : `O(max⁡(m,n)`
時間複雜度: O(max⁡(m,n)) ,其中 m 和 n 分別為兩個鏈表的長度。 我們要遍歷兩個鏈表的全部位置,而處理每個位置只需要 O(1) 的時間

* 空間複雜 : `O(1)`
O(1) 。 注意返回值不計入空間複雜度

## 來源
* https://leetcode.com/problems/add-two-numbers/description/
* https://leetcode.cn/problems/

## 解答
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0002.Add-Two-Numbers/main.go

```go
package addtwonumbers

// 時間複雜 O(max(m,n)), 空間複雜 O(1)
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/

// 遍歷 l1跟 l2. 講兩個list的val相加, 並且記錄進位的值給next使用
// 最後如果 carry 還有的話, 需要產生一個新的節點
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var result, tail *ListNode
carry := 0
for l1 != nil || l2 != nil {
n1, n2 := 0, 0
if l1 != nil {
n1 = l1.Val
l1 = l1.Next
}
if l2 != nil {
n2 = l2.Val
l2 = l2.Next
}
sum := n1 + n2 + carry
sum, carry = sum%10, sum/10

if result == nil {
result = &ListNode{Val: sum, Next: nil}
tail = result
} else {
tail.Next = &ListNode{Val: sum, Next: nil}
tail = tail.Next
}
}
// 最後如果 carry 還有的話, 需要產生一個新的節點
if carry > 0 {
tail.Next = &ListNode{Val: carry, Next: nil}
}
return result
}
```

## Benchmark

```sh

```
43 changes: 43 additions & 0 deletions Leetcode/0002.Add-Two-Numbers/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package addtwonumbers

// 時間複雜 O(max(m,n)), 空間複雜 O(1)
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/

// 遍歷 l1跟 l2. 講兩個list的val相加, 並且記錄進位的值給next使用
// 最後如果 carry 還有的話, 需要產生一個新的節點
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
var result, tail *ListNode
carry := 0
for l1 != nil || l2 != nil {
n1, n2 := 0, 0
if l1 != nil {
n1 = l1.Val
l1 = l1.Next
}
if l2 != nil {
n2 = l2.Val
l2 = l2.Next
}
sum := n1 + n2 + carry
sum, carry = sum%10, sum/10

if result == nil {
result = &ListNode{Val: sum, Next: nil}
tail = result
} else {
tail.Next = &ListNode{Val: sum, Next: nil}
tail = tail.Next
}
}
// 最後如果 carry 還有的話, 需要產生一個新的節點
if carry > 0 {
tail.Next = &ListNode{Val: carry, Next: nil}
}
return result
}
35 changes: 35 additions & 0 deletions Leetcode/0002.Add-Two-Numbers/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// TODO : 0002.Add-Two-Numbers Unit Test
package

import "testing"

var tests = []struct {
arg1 string
want int
}{
{
"bbbab",
4,
},
}

func TestLongestPalindromeSubseq(t *testing.T) {
for _, tt := range tests {
// if got := ReverseList(tt.arg1); !reflect.DeepEqual(got, tt.want) {
if got := LongestPalindromeSubseq(tt.arg1); got != tt.want {
t.Errorf("got = %v, want = %v", got, tt.want)
}
}
}

func BenchmarkLongestPalindromeSubseq(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
LongestPalindromeSubseq(tests[0].arg1)
}
}

/*
go test -benchmem -run=none LeetcodeGolang/Leetcode/0354.Russian-Doll-Envelopes -bench=.
*/
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,16 @@ https://kimi0230.github.io/LeetcodeGolang/
---
#### Linked List

| No. | Title | Solution | Difficulty | Time | Space | Topic |
|---------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------------------:|------------|----------|-------|---------------------------|
| [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 |
| [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 |
| [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 |
| [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 |
| [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 |
| [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 |
| [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 |
| No. | Title | Solution | Difficulty | Time | Space | Topic |
|---------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------------------:|------------|-------------|-------|---------------------------|
| [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 |
| [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 |
| [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 |
| [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 |
| [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 |
| [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 |
| [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 |
| [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 |

---

Expand Down

0 comments on commit 1ea7649

Please sign in to comment.