From 7b236b56b620c8bc218b6c23d7d8550408f05ae4 Mon Sep 17 00:00:00 2001 From: YDZ Date: Tue, 19 Feb 2019 13:42:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2.Add-Two-Number/Add Two Numbers.go | 43 +++++++ .../2.Add-Two-Number/Add Two Numbers_test.go | 111 ++++++++++++++++++ Algorithms/2.Add-Two-Number/README.md | 30 +++++ 3 files changed, 184 insertions(+) create mode 100644 Algorithms/2.Add-Two-Number/Add Two Numbers.go create mode 100644 Algorithms/2.Add-Two-Number/Add Two Numbers_test.go create mode 100644 Algorithms/2.Add-Two-Number/README.md diff --git a/Algorithms/2.Add-Two-Number/Add Two Numbers.go b/Algorithms/2.Add-Two-Number/Add Two Numbers.go new file mode 100644 index 00000000..0b85531e --- /dev/null +++ b/Algorithms/2.Add-Two-Number/Add Two Numbers.go @@ -0,0 +1,43 @@ +package leetcode + +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { + if l1 == nil || l2 == nil { + return nil + } + head := &ListNode{Val: 0, Next: nil} + current := head + carry := 0 + for l1 != nil || l2 != nil { + var x, y int + if l1 == nil { + x = 0 + } else { + x = l1.Val + } + if l2 == nil { + y = 0 + } else { + y = l2.Val + } + current.Next = &ListNode{Val: (x + y + carry) % 10, Next: nil} + current = current.Next + carry = (x + y + carry) / 10 + if l1 != nil { + l1 = l1.Next + } + if l2 != nil { + l2 = l2.Next + } + } + if carry > 0 { + current.Next = &ListNode{Val: carry % 10, Next: nil} + } + return head.Next +} diff --git a/Algorithms/2.Add-Two-Number/Add Two Numbers_test.go b/Algorithms/2.Add-Two-Number/Add Two Numbers_test.go new file mode 100644 index 00000000..24d9d9fd --- /dev/null +++ b/Algorithms/2.Add-Two-Number/Add Two Numbers_test.go @@ -0,0 +1,111 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question2 struct { + para2 + ans2 +} + +// para 是参数 +// one 代表第一个参数 +type para2 struct { + one []int + another []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans2 struct { + one []int +} + +func Test_Problem2(t *testing.T) { + + qs := []question2{ + + question2{ + para2{[]int{}, []int{}}, + ans2{[]int{}}, + }, + + question2{ + para2{[]int{1}, []int{1}}, + ans2{[]int{2}}, + }, + + question2{ + para2{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}}, + ans2{[]int{2, 4, 6, 8}}, + }, + + question2{ + para2{[]int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}}, + ans2{[]int{2, 4, 6, 8, 0, 1}}, + }, + + question2{ + para2{[]int{1}, []int{9, 9, 9, 9, 9}}, + ans2{[]int{0, 0, 0, 0, 0, 1}}, + }, + + question2{ + para2{[]int{9, 9, 9, 9, 9}, []int{1}}, + ans2{[]int{0, 0, 0, 0, 0, 1}}, + }, + + question2{ + para2{[]int{2, 4, 3}, []int{5, 6, 4}}, + ans2{[]int{7, 0, 8}}, + }, + + question2{ + para2{[]int{1, 8, 3}, []int{7, 1}}, + ans2{[]int{8, 9, 3}}, + }, + // 如需多个测试,可以复制上方元素。 + } + + fmt.Printf("------------------------Leetcode Problem 2------------------------\n") + + for _, q := range qs { + _, p := q.ans2, q.para2 + fmt.Printf("【input】:%v 【output】:%v\n", p, L2s(addTwoNumbers(S2l(p.one), S2l(p.another)))) + } + fmt.Printf("\n\n\n") +} + +// // convert *ListNode to []int +// func L2s(head *ListNode) []int { +// res := []int{} + +// for head != nil { +// res = append(res, head.Val) +// head = head.Next +// } + +// return res +// } + +// // convert []int to *ListNode +// func S2l(nums []int) *ListNode { +// if len(nums) == 0 { +// return nil +// } + +// res := &ListNode{ +// Val: nums[0], +// } +// temp := res +// for i := 1; i < len(nums); i++ { +// temp.Next = &ListNode{ +// Val: nums[i], +// } +// temp = temp.Next +// } + +// return res +// } diff --git a/Algorithms/2.Add-Two-Number/README.md b/Algorithms/2.Add-Two-Number/README.md new file mode 100644 index 00000000..e51ab250 --- /dev/null +++ b/Algorithms/2.Add-Two-Number/README.md @@ -0,0 +1,30 @@ +# [2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) + +## 题目 + +You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order 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. + +Example: + +``` +Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) +Output: 7 -> 0 -> 8 +``` +Explanation: 342 + 465 = 807. + + +## 题目大意 + +2 个逆序的链表,要求从低位开始相加,得出结果也逆序输出,返回值是逆序结果链表的头结点。 + +需要注意的是各种进位问题。 + +极端情况,例如 +``` +Input: (9 -> 9 -> 9 -> 9 -> 9) + (1 -> ) +Output: 0 -> 0 -> 0 -> 0 -> 0 -> 1 +``` + +为了处理方法统一,可以先建立一个虚拟头结点,这个虚拟头结点的 Next 指向真正的 head,这样 head 不需要单独处理,直接 while 循环即可。另外判断循环终止的条件不用是 p.Next != nil,这样最后一位还需要额外计算,循环终止条件应该是 p != nil。 \ No newline at end of file