-
Notifications
You must be signed in to change notification settings - Fork 0
/
041_FirstMissingPositive41.java
48 lines (39 loc) · 1.09 KB
/
041_FirstMissingPositive41.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
class Solution {
public int firstMissingPositive(int[] A) {
int n = A.length;
boolean[] present = new boolean[n + 1];
for (int i = 0; i < n; i++)
{
if (A[i] > 0 && A[i] <= n)
present[A[i]] = true;
}
for (int i = 1; i <= n; i++)
if (!present[i])
return i;
return n + 1;
}
}
*******************************************************************************************
class Solution {
public int firstMissingPositive(int[] nums) {
// Solving using the cyclic sort pattern
int i=0;
while(i < nums.length) {
if(nums[i] > 0 && nums[i] <= nums.length && nums[i] != nums[nums[i] - 1])
{
int temp = nums[i];
nums[i] = nums[temp - 1];
nums[temp - 1] = temp;
} else {
i++;
}
}
for(i = 0; i< nums.length; i++) {
if(nums[i] != i + 1) {
return i+1;
}
}
return nums.length + 1;
}
}
******************************************************************************************