-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (82 loc) · 2.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
)
// Given an unsorted integer array, find the first missing positive integer.
//
// For example,
// Given [1,2,0] return 3,
// and [3,4,-1,1] return 2.
//
// Your algorithm should run in O(n) time and uses constant space.
// shit! Nums may have duplicate positive integer
// Note: not right!
func firstMissingPositiveWrong(nums []int) int {
var sum int
var count int
for _, n := range nums {
if n > 0 {
sum += n
if n > count {
count = n
}
}
}
ret := count*(count+1)/2 - sum
if ret == 0 {
return count + 1
}
return ret
}
/*
ref: https://discuss.leetcode.com/topic/10351/o-1-space-java-solution
The key here is to use swapping to keep constant space and also make use of the length of the array,
which means there can be at most n positive integers. So each time we encounter an valid integer,
find its correct position and swap. Otherwise we continue.
public class Solution {
public int firstMissingPositive(int[] A) {
int i = 0;
while(i < A.length){
if(A[i] == i+1 || A[i] <= 0 || A[i] > A.length) i++;
else if(A[A[i]-1] != A[i]) swap(A, i, A[i]-1);
else i++;
}
i = 0;
while(i < A.length && A[i] == i+1) i++;
return i+1;
}
private void swap(int[] A, int i, int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
*/
func firstMissingPositive(nums []int) int {
i := 0
for i < len(nums) {
if nums[i] == i+1 || nums[i] <= 0 || nums[i] > len(nums) {
i++
} else if nums[i] != nums[nums[i]-1] {
j := nums[i] - 1
nums[i], nums[j] = nums[j], nums[i]
} else {
// duplicate element
i++
}
}
i = 0
for i < len(nums) && nums[i] == i+1 {
i++
}
return i + 1
}
func main() {
// show result
fmt.Println(firstMissingPositive([]int{1, 2, 0}))
fmt.Println(firstMissingPositive([]int{3, 4, -1, 1}))
fmt.Println(firstMissingPositive([]int{1, 1}))
fmt.Println(firstMissingPositive([]int{2, 2}))
fmt.Println(firstMissingPositive([]int{}))
fmt.Println(firstMissingPositive([]int{1}))
}