Skip to content

Commit 1f5d201

Browse files
author
王俊超
committed
commit
1 parent 5916c62 commit 1f5d201

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @author: wangjunchao(王俊超)
3+
* @time: 2018-09-28 15:18
4+
**/
5+
public class Solution {
6+
public int firstMissingPositive(int[] nums) {
7+
int n = nums.length;
8+
for (int i = 0; i < n; i++) {
9+
while (nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
10+
swap(nums, i, nums[i] - 1);
11+
}
12+
}
13+
for (int i = 0; i < n; i++) {
14+
if (nums[i] != i + 1) {
15+
return i + 1;
16+
}
17+
}
18+
19+
return n + 1;
20+
}
21+
22+
private void swap(int[] nums, int i, int j) {
23+
int temp = nums[i];
24+
nums[i] = nums[j];
25+
nums[j] = temp;
26+
}
27+
}

0 commit comments

Comments
 (0)