-
Notifications
You must be signed in to change notification settings - Fork 3
/
_0041_FirstMissingPositive.java
84 lines (77 loc) · 2.1 KB
/
_0041_FirstMissingPositive.java
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
package com.diguage.algorithm.leetcode;
import java.util.Objects;
/**
* = 41. First Missing Positive
*
* https://leetcode.com/problems/first-missing-positive/[First Missing Positive - LeetCode]
*
* Given an unsorted integer array, find the smallest missing positive integer.
*
* .Example 1:
* [source]
* ----
* Input: [1,2,0]
* Output: 3
* ----
*
* .Example 2:
* [source]
* ----
* Input: [3,4,-1,1]
* Output: 2
* ----
*
* .Example 3:
* [source]
* ----
* Input: [7,8,9,11,12]
* Output: 1
* ----
*
* *Note:*
*
* Your algorithm should run in O(n) time and uses constant extra space.
*
* @author D瓜哥, https://www.diguage.com/
* @since 2019-10-23 12:54:29
*/
public class _0041_FirstMissingPositive {
/**
* Runtime: 0 ms, faster than 100.00% of Java online submissions for First Missing Positive.
*
* Memory Usage: 34.6 MB, less than 100.00% of Java online submissions for First Missing Positive.
*/
public int firstMissingPositive(int[] nums) {
if (Objects.isNull(nums) || nums.length == 0) {
return 1;
}
int length = nums.length;
for (int i = 0; i < length; ) {
int iNum = nums[i];
if (0 < iNum && iNum <= length && nums[iNum - 1] != iNum) {
swap(nums, i, iNum - 1);
} else {
i++;
}
}
int i = 0;
while (i < length && nums[i] == i + 1) {
i++;
}
return i + 1;
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public static void main(String[] args) {
_0041_FirstMissingPositive solution = new _0041_FirstMissingPositive();
int i1 = solution.firstMissingPositive(new int[]{1, 2, 0});
System.out.println((i1 == 3) + ", " + i1);
int i2 = solution.firstMissingPositive(new int[]{3, 4, -1, 1});
System.out.println((i2 == 2) + ", " + i2);
int i3 = solution.firstMissingPositive(new int[]{7, 8, 9, 11, 12});
System.out.println((i3 == 1) + ", " + i3);
}
}