Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6759a4b

Browse files
refactor 41
1 parent 48d39d4 commit 6759a4b

File tree

1 file changed

+6
-2
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+6
-2
lines changed

src/main/java/com/fishercoder/solutions/_41.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
*41. First Missing Positive
45
* Given an unsorted integer array, find the first missing positive integer.
56
67
For example,
78
Given [1,2,0] return 3,
89
and [3,4,-1,1] return 2.
910
1011
Your algorithm should run in O(n) time and uses constant space.
11-
1212
*/
13+
1314
public class _41 {
1415

1516
public int firstMissingPositive(int[] A) {
1617
int i = 0;
1718
while (i < A.length) {
18-
if (A[i] > 0 && A[i] != i + 1 && A[i] - 1 < A.length
19+
if (A[i] > 0 && A[i] != i + 1
20+
&& A[i] - 1 < A.length
1921
&& A[i] != A[A[i] - 1]) {
2022
swap(A, i, A[i] - 1);
2123
} else {
2224
i++;
2325
}
2426
}
27+
2528
for (int j = 0; j < A.length; j++) {
2629
if (A[j] != j + 1) {
2730
return j + 1;
2831
}
2932
}
33+
3034
return A.length + 1;// if all values are in the correct position, then
3135
// we return the last one + 1
3236
}

0 commit comments

Comments
 (0)