50 lines
813 B
Go
50 lines
813 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
)
|
|
|
|
func main() {
|
|
|
|
fmt.Println(firstMissingPositive([]int{1000, -1}))
|
|
}
|
|
func firstMissingPositive(nums []int) int {
|
|
if len(nums) == 1 {
|
|
if nums[0] == 1 {
|
|
return 2
|
|
}
|
|
return 1
|
|
}
|
|
numlist := make([]int, 0)
|
|
for i := 0; i < len(nums); i++ {
|
|
if nums[i] > 0 {
|
|
numlist = append(numlist, nums[i])
|
|
}
|
|
}
|
|
|
|
sort.Ints(numlist)
|
|
fmt.Println(numlist)
|
|
slow, fast := 0, 1
|
|
if len(numlist) == 0 {
|
|
return 1
|
|
}
|
|
if numlist[0] != 1 {
|
|
return 1
|
|
}
|
|
for slow <= fast && slow <= len(numlist)-1 && fast <= len(numlist)-1 {
|
|
if numlist[fast] == numlist[slow]+1 || numlist[fast] == numlist[slow] {
|
|
fmt.Println("相等")
|
|
fmt.Println("当前", numlist[fast], numlist[slow]+1)
|
|
slow++
|
|
fast = slow + 1
|
|
|
|
continue
|
|
} else {
|
|
break
|
|
}
|
|
|
|
}
|
|
return numlist[slow] + 1
|
|
}
|