Skip to content

Commit 486f447

Browse files
refactor 41
1 parent 8279086 commit 486f447

File tree

3 files changed

+70
-27
lines changed

3 files changed

+70
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ Your ideas/fixes/algorithms are more than welcome!
631631
|44|[Wildcard Matching](https://leetcode.com/problems/wildcard-matching/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_44.java)|O(m*n)|O(m*n)||Hard| Backtracking, DP, Greedy, String
632632
|43|[Multiply Strings](https://leetcode.com/problems/multiply-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_43.java)|O(n)|O(1)||Medium| Array, String
633633
|42|[Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_42.java)|O(n)|O(1)||Hard|
634-
|41|[First Missing Positive](https://leetcode.com/problems/first-missing-positive/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_41.java)|O(n)|O(1)||Hard|
634+
|41|[First Missing Positive](https://leetcode.com/problems/first-missing-positive/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_41.java)|O(n)|O(1)||Hard| Array
635635
|40|[Combination Sum II](https://leetcode.com/problems/combination-sum-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_40.java)|O(k*n^k)|O(k)||Medium|Backtracking
636636
|39|[Combination Sum](https://leetcode.com/problems/combination-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_39.java)|O(k*n^k)|O(k)||Medium|Backtracking
637637
|38|[Count and Say](https://leetcode.com/problems/count-and-say/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_38.java)|O(n*2^n)|O(2^n)||Easy| Recursion, LinkedList

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/**
44
*41. First Missing Positive
5+
*
56
*Given an unsorted integer array, find the first missing positive integer.
67
78
For example,
@@ -13,35 +14,36 @@ Your algorithm should run in O(n) time and uses constant space.
1314

1415
public class _41 {
1516

16-
public static class Solution1 {
17-
public int firstMissingPositive(int[] nums) {
18-
int i = 0;
19-
while (i < nums.length) {
20-
if (nums[i] > 0 && nums[i] != i + 1
21-
&& nums[i] - 1 < nums.length
22-
&& nums[i] != nums[nums[i] - 1]) {
23-
swap(nums, i, nums[i] - 1);
24-
} else {
25-
i++;
26-
}
27-
}
28-
29-
for (int j = 0; j < nums.length; j++) {
30-
if (nums[j] != j + 1) {
31-
return j + 1;
32-
}
33-
}
34-
35-
return nums.length + 1;
36-
/** if all values are in the correct position, then we return the length + 1.
37-
* This also takes care of corner case: [], we return 1 for it.*/
17+
public static class Solution1 {
18+
/**
19+
* Time: O(n) Space: O(1)
20+
*
21+
* Idea: put every number in its right position, e.g. put 5 in nums[4].
22+
*/
23+
public int firstMissingPositive(int[] nums) {
24+
int i = 0;
25+
while (i < nums.length) {
26+
if (nums[i] > 0 && nums[i] != i + 1 && nums[i] - 1 < nums.length && nums[i] != nums[nums[i]
27+
- 1]) {
28+
swap(nums, i, nums[i] - 1);
29+
} else {
30+
i++;
3831
}
32+
}
3933

40-
public void swap(int[] nums, int i, int j) {
41-
int temp = nums[i];
42-
nums[i] = nums[j];
43-
nums[j] = temp;
34+
for (int j = 0; j < nums.length; j++) {
35+
if (nums[j] != j + 1) {
36+
return j + 1;
4437
}
38+
}
39+
40+
return nums.length + 1;
4541
}
4642

43+
void swap(int[] nums, int i, int j) {
44+
int temp = nums[i];
45+
nums[i] = nums[j];
46+
nums[j] = temp;
47+
}
48+
}
4749
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._41;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _41Test {
10+
private static _41.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _41.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[] {1, 2, 0};
21+
assertEquals(3, solution1.firstMissingPositive(nums));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[] {};
27+
assertEquals(1, solution1.firstMissingPositive(nums));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
nums = new int[] {3, 4, -1, 1};
33+
assertEquals(2, solution1.firstMissingPositive(nums));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
nums = new int[] {2};
39+
assertEquals(1, solution1.firstMissingPositive(nums));
40+
}
41+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy