From 9e03e1a7deca3dfcd72fdab42972d2ffabd25a85 Mon Sep 17 00:00:00 2001 From: YDZ Date: Wed, 3 Jul 2019 17:44:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=20problem=200001?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0001. Two Sum/1. Add Two Numbers.go | 43 ---------- .../0001. Two Sum/1. Add Two Numbers_test.go | 79 ------------------- Algorithms/0001. Two Sum/1. Two Sum.go | 13 +++ Algorithms/0001. Two Sum/1. Two Sum_test.go | 34 ++++++++ 4 files changed, 47 insertions(+), 122 deletions(-) delete mode 100644 Algorithms/0001. Two Sum/1. Add Two Numbers.go delete mode 100644 Algorithms/0001. Two Sum/1. Add Two Numbers_test.go create mode 100644 Algorithms/0001. Two Sum/1. Two Sum.go create mode 100644 Algorithms/0001. Two Sum/1. Two Sum_test.go diff --git a/Algorithms/0001. Two Sum/1. Add Two Numbers.go b/Algorithms/0001. Two Sum/1. Add Two Numbers.go deleted file mode 100644 index 0b85531e..00000000 --- a/Algorithms/0001. Two Sum/1. Add Two Numbers.go +++ /dev/null @@ -1,43 +0,0 @@ -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/0001. Two Sum/1. Add Two Numbers_test.go b/Algorithms/0001. Two Sum/1. Add Two Numbers_test.go deleted file mode 100644 index 304ffa76..00000000 --- a/Algorithms/0001. Two Sum/1. Add Two Numbers_test.go +++ /dev/null @@ -1,79 +0,0 @@ -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") -} diff --git a/Algorithms/0001. Two Sum/1. Two Sum.go b/Algorithms/0001. Two Sum/1. Two Sum.go new file mode 100644 index 00000000..dda3d53f --- /dev/null +++ b/Algorithms/0001. Two Sum/1. Two Sum.go @@ -0,0 +1,13 @@ +package leetcode + +func twoSum(nums []int, target int) []int { + m := make(map[int]int) + for i := 0; i < len(nums); i++ { + another := target - nums[i] + if _, ok := m[another]; ok { + return []int{m[another], i} + } + m[nums[i]] = i + } + return nil +} diff --git a/Algorithms/0001. Two Sum/1. Two Sum_test.go b/Algorithms/0001. Two Sum/1. Two Sum_test.go new file mode 100644 index 00000000..3270cc75 --- /dev/null +++ b/Algorithms/0001. Two Sum/1. Two Sum_test.go @@ -0,0 +1,34 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +func TestTwoSum(t *testing.T) { + tests := [][]int{ + []int{3, 2, 4}, + []int{3, 2, 4}, + []int{0, 8, 7, 3, 3, 4, 2}, + []int{0, 1}, + } + targets := []int{ + 6, + 5, + 11, + 1, + } + results := [][]int{ + []int{1, 2}, + []int{0, 1}, + []int{1, 3}, + []int{0, 1}, + } + fmt.Printf("------------------------Leetcode Problem 1------------------------\n") + for i := 0; i < len(targets); i++ { + fmt.Printf("nums = %v target = %v result = %v\n", tests[i], targets[i], twoSum(tests[i], targets[i])) + if ret := twoSum(tests[i], targets[i]); ret[0] != results[i][0] && ret[1] != results[i][1] { + t.Fatalf("case %d fails: %v\n", i, ret) + } + } +}