diff --git a/Algorithms/0445. Add Two Numbers II/445. Add Two Numbers II.go b/Algorithms/0445. Add Two Numbers II/445. Add Two Numbers II.go new file mode 100644 index 00000000..efbc5329 --- /dev/null +++ b/Algorithms/0445. Add Two Numbers II/445. Add Two Numbers II.go @@ -0,0 +1,62 @@ +package leetcode + +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func addTwoNumbers_(l1 *ListNode, l2 *ListNode) *ListNode { + if l1 == nil { + return l2 + } + if l2 == nil { + return l1 + } + l1Length := getLength(l1) + l2Length := getLength(l2) + newHeader := &ListNode{Val: 1, Next: nil} + if l1Length < l2Length { + newHeader.Next = addNode(l2, l1, l2Length-l1Length) + } else { + newHeader.Next = addNode(l1, l2, l1Length-l2Length) + } + if newHeader.Next.Val > 9 { + newHeader.Next.Val = newHeader.Next.Val % 10 + return newHeader + } + return newHeader.Next +} + +func addNode(l1 *ListNode, l2 *ListNode, offset int) *ListNode { + if l1 == nil { + return nil + } + var ( + res, node *ListNode + ) + if offset == 0 { + res = &ListNode{Val: l1.Val + l2.Val, Next: nil} + node = addNode(l1.Next, l2.Next, 0) + } else { + res = &ListNode{Val: l1.Val, Next: nil} + node = addNode(l1.Next, l2, offset-1) + } + if node != nil && node.Val > 9 { + res.Val++ + node.Val = node.Val % 10 + } + res.Next = node + return res +} + +func getLength(l *ListNode) int { + count := 0 + cur := l + for cur != nil { + count++ + cur = cur.Next + } + return count +} diff --git a/Algorithms/0445. Add Two Numbers II/445. Add Two Numbers II_test.go b/Algorithms/0445. Add Two Numbers II/445. Add Two Numbers II_test.go new file mode 100644 index 00000000..c036c825 --- /dev/null +++ b/Algorithms/0445. Add Two Numbers II/445. Add Two Numbers II_test.go @@ -0,0 +1,79 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question445 struct { + para445 + ans445 +} + +// para 是参数 +// one 代表第一个参数 +type para445 struct { + one []int + another []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans445 struct { + one []int +} + +func Test_Problem445(t *testing.T) { + + qs := []question445{ + + question445{ + para445{[]int{}, []int{}}, + ans445{[]int{}}, + }, + + question445{ + para445{[]int{1}, []int{1}}, + ans445{[]int{2}}, + }, + + question445{ + para445{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}}, + ans445{[]int{2, 4, 6, 8}}, + }, + + question445{ + para445{[]int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}}, + ans445{[]int{2, 4, 6, 9, 0}}, + }, + + question445{ + para445{[]int{1}, []int{9, 9, 9, 9, 9}}, + ans445{[]int{1, 0, 0, 0, 0, 0}}, + }, + + question445{ + para445{[]int{9, 9, 9, 9, 9}, []int{1}}, + ans445{[]int{1, 0, 0, 0, 0, 0}}, + }, + + question445{ + para445{[]int{2, 4, 3}, []int{5, 6, 4}}, + ans445{[]int{8, 0, 7}}, + }, + + question445{ + para445{[]int{1, 8, 3}, []int{7, 1}}, + ans445{[]int{2, 5, 4}}, + }, + // 如需多个测试,可以复制上方元素。 + } + + fmt.Printf("------------------------Leetcode Problem 445------------------------\n") + + for _, q := range qs { + _, p := q.ans445, q.para445 + fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(addTwoNumbers_(S2l(p.one), S2l(p.another)))) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0445. Add Two Numbers II/README.md b/Algorithms/0445. Add Two Numbers II/README.md new file mode 100644 index 00000000..16eb534e --- /dev/null +++ b/Algorithms/0445. Add Two Numbers II/README.md @@ -0,0 +1,23 @@ +# [445. Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/) + +## 题目 + +You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. + +You may assume the two numbers do not contain any leading zero, except the number 0 itself. + +Follow up: +What if you cannot modify the input lists? In other words, reversing the lists is not allowed. + +Example: + +```text +Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) +Output: 7 -> 8 -> 0 -> 7 +``` + +## 题目大意 + +这道题是第 2 题的变种题,第 2 题中的 2 个数是从个位逆序排到高位,这样相加只用从头交到尾,进位一直进位即可。这道题目中强制要求不能把链表逆序。2 个数字是从高位排到低位的,这样进位是倒着来的。 + +思路也不难,加法只用把短的链表依次加到长的链表上面来就可以了,最终判断一下最高位有没有进位,有进位再往前进一位。加法的过程可以用到递归。 \ No newline at end of file