diff --git a/Algorithms/41.First-Missing-Positive/First Missing Positive.go b/Algorithms/41.First-Missing-Positive/First Missing Positive.go new file mode 100644 index 00000000..21dbbf4e --- /dev/null +++ b/Algorithms/41.First-Missing-Positive/First Missing Positive.go @@ -0,0 +1,14 @@ +package leetcode + +func firstMissingPositive(nums []int) int { + numMap := make(map[int]int, len(nums)) + for _, v := range nums { + numMap[v] = v + } + for index := 1; index < len(nums)+1; index++ { + if _, ok := numMap[index]; !ok { + return index + } + } + return len(nums) + 1 +} diff --git a/Algorithms/41.First-Missing-Positive/First Missing Positive_test.go b/Algorithms/41.First-Missing-Positive/First Missing Positive_test.go new file mode 100644 index 00000000..e1d0ba59 --- /dev/null +++ b/Algorithms/41.First-Missing-Positive/First Missing Positive_test.go @@ -0,0 +1,71 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question41 struct { + para41 + ans41 +} + +// para 是参数 +// one 代表第一个参数 +type para41 struct { + one []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans41 struct { + one int +} + +func Test_Problem41(t *testing.T) { + + qs := []question41{ + + question41{ + para41{[]int{10, -1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, -3}}, + ans41{6}, + }, + + question41{ + para41{[]int{10, -1, 8, 6, 7, 3, -2, 5, 4, 2, 1, -3}}, + ans41{9}, + }, + + question41{ + para41{[]int{1}}, + ans41{2}, + }, + + question41{ + para41{[]int{0, 2, 2, 1, 1}}, + ans41{3}, + }, + + question41{ + para41{[]int{}}, + ans41{1}, + }, + + question41{ + para41{[]int{1, 2, 0}}, + ans41{3}, + }, + + question41{ + para41{[]int{3, 4, -1, 1}}, + ans41{2}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 41------------------------\n") + for _, q := range qs { + _, p := q.ans41, q.para41 + fmt.Printf("【input】:%v 【output】:%v\n", p, firstMissingPositive(p.one)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/41.First-Missing-Positive/README.md b/Algorithms/41.First-Missing-Positive/README.md new file mode 100644 index 00000000..23b3d5e9 --- /dev/null +++ b/Algorithms/41.First-Missing-Positive/README.md @@ -0,0 +1,21 @@ +# [41. First Missing Positive](https://leetcode.com/problems/first-missing-positive/description/) + +## 题目 + +Given an unsorted integer array, find the smallest missing positive integer. + +Example 1: + +Input: [1,2,0] +Output: 3 +Example 2: + +Input: [3,4,-1,1] +Output: 2 +Example 3: + +Input: [7,8,9,11,12] +Output: 1 +Note: + +Your algorithm should run in O(n) time and uses constant extra space. \ No newline at end of file diff --git a/Algorithms/412.Fizz-Buzz/Fizz Buzz_test.go b/Algorithms/412.Fizz-Buzz/Fizz Buzz_test.go index 442a3167..35c71c8c 100644 --- a/Algorithms/412.Fizz-Buzz/Fizz Buzz_test.go +++ b/Algorithms/412.Fizz-Buzz/Fizz Buzz_test.go @@ -33,9 +33,11 @@ var tcs = []struct { } func Test_fizzBuzz(t *testing.T) { + fmt.Printf("------------------------Leetcode Problem 412------------------------\n") for _, tc := range tcs { - fmt.Printf("%v\n", tc) + fmt.Printf("【output】:%v\n", tc) } + fmt.Printf("\n\n\n") } func Benchmark_fizzBuzz(b *testing.B) { diff --git a/Algorithms/88.Merge-Sorted-Array/Merge Sorted Array_test.go b/Algorithms/88.Merge-Sorted-Array/Merge Sorted Array_test.go index d3c4e5a7..3d078d12 100644 --- a/Algorithms/88.Merge-Sorted-Array/Merge Sorted Array_test.go +++ b/Algorithms/88.Merge-Sorted-Array/Merge Sorted Array_test.go @@ -5,14 +5,14 @@ import ( "testing" ) -type question struct { - para - ans +type question88 struct { + para88 + ans88 } // para 是参数 // one 代表第一个参数 -type para struct { +type para88 struct { one []int m int two []int @@ -21,13 +21,13 @@ type para struct { // ans 是答案 // one 代表第一个答案 -type ans struct { +type ans88 struct { one []int } -func Test_Problem0088(t *testing.T) { +func Test_Problem88(t *testing.T) { - qs := []question{ + qs := []question88{ // question{ // para{[]int{0}, 0, []int{1}, 1}, @@ -44,16 +44,17 @@ func Test_Problem0088(t *testing.T) { // ans{[]int{1, 2, 2, 3}}, // }, - question{ - para{[]int{1, 2, 3, 0, 0, 0}, 3, []int{2, 5, 6}, 3}, - ans{[]int{1, 2, 2, 3}}, + question88{ + para88{[]int{1, 2, 3, 0, 0, 0}, 3, []int{2, 5, 6}, 3}, + ans88{[]int{1, 2, 2, 3, 5, 6}}, }, } - + fmt.Printf("------------------------Leetcode Problem 88------------------------\n") for _, q := range qs { - _, p := q.ans, q.para - + _, p := q.ans88, q.para88 + fmt.Printf("【intput】:%v,%v,%v,%v ", p.one, p.m, p.two, p.n) merge(p.one, p.m, p.two, p.n) - fmt.Printf("~~%v~~\n", p) + fmt.Printf("【output】:%v\n", p) } + fmt.Printf("\n\n\n") }