mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
添加 problem 44
This commit is contained in:
@ -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
|
||||
}
|
@ -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")
|
||||
}
|
21
Algorithms/41.First-Missing-Positive/README.md
Normal file
21
Algorithms/41.First-Missing-Positive/README.md
Normal file
@ -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.
|
@ -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) {
|
||||
|
@ -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")
|
||||
}
|
||||
|
Reference in New Issue
Block a user