Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

41. First Missing Positive

Posted on 2020-06-28 | Edited on 2021-01-26

LeetCode

Array in-place operation link

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int firstMissingPositive(int[] nums) {
int n = nums.length;
for (int i = 0; i < n; i++) {
while (nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] != nums[i]) {
int next = nums[nums[i] - 1];
nums[nums[i] - 1] = nums[i];
nums[i] = next;
}
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] != i + 1) return i+1;
}
return nums.length + 1;
}
}

<1…127128129…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%