-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0041.java
More file actions
31 lines (26 loc) · 853 Bytes
/
Copy pathLeetCode0041.java
File metadata and controls
31 lines (26 loc) · 853 Bytes
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
/* First Missing Positive
* Input: nums = [3,4,-1,1]
* Output: 2
* */
public class LeetCode0041 {
public static void main(String args[]) {
int[] nums = {3, 4, -1, 1};
System.out.println(firstMissingPositive(nums));
}
public static int firstMissingPositive(int[] nums) {
if (nums == null || nums.length == 0)
return 1;
for (int i = 0; i < nums.length; i++) {
while (nums[i] > 0 && nums[i] <= nums.length && nums[i] != nums[nums[i] - 1]){
int temp = nums[nums[i] - 1];
nums[nums[i] - 1] = nums[i];
nums[i] = temp;
}
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] != i + 1)
return i + 1;
}
return nums.length + 1;
}
}