Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cc7ce3d

Browse files
committed
solve first missing positive
1 parent 7098087 commit cc7ce3d

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

‎algs/0041.first_missing_positive.go‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package algs
2+
3+
// FirstMissingPositive
4+
//
5+
// Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums.
6+
//
7+
// You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.
8+
//
9+
// Example 1:
10+
//
11+
// Input: nums = [1,2,0]
12+
// Output: 3
13+
// Explanation: The numbers in the range [1,2] are all in the array.
14+
//
15+
// Example 2:
16+
//
17+
// Input: nums = [3,4,-1,1]
18+
// Output: 2
19+
// Explanation: 1 is in the array but 2 is missing.
20+
//
21+
// Example 3:
22+
//
23+
// Input: nums = [7,8,9,11,12]
24+
// Output: 1
25+
// Explanation: The smallest positive integer 1 is missing.
26+
//
27+
// Constraints:
28+
//
29+
// 1 <= nums.length <= 10^5
30+
// -2^31 <= nums[i] <= 2^31 - 1
31+
func FirstMissingPositive(nums []int) int {
32+
for i := 0; i < len(nums); i++ {
33+
if nums[i] == 0 {
34+
nums[i] = -1
35+
}
36+
}
37+
38+
for i := 0; i < len(nums); i++ {
39+
for nums[i] >= 1 && nums[i] <= len(nums) {
40+
j := nums[i] - 1
41+
tmp := nums[j]
42+
nums[j] = 0
43+
if i != j {
44+
if tmp != 0 {
45+
nums[i] = tmp
46+
} else {
47+
nums[i] = -1
48+
}
49+
}
50+
}
51+
}
52+
53+
for i := 0; i < len(nums); i++ {
54+
if nums[i] != 0 {
55+
return i + 1
56+
}
57+
}
58+
59+
return len(nums) + 1
60+
}

‎src/solution.rs‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,4 +873,63 @@ impl Solution {
873873
}
874874
result
875875
}
876+
877+
/// [41. Frist Missing Positive]
878+
///
879+
/// Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums.
880+
///
881+
/// You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.
882+
///
883+
/// Example 1:
884+
///
885+
/// Input: nums = [1,2,0]
886+
/// Output: 3
887+
/// Explanation: The numbers in the range [1,2] are all in the array.
888+
///
889+
/// Example 2:
890+
///
891+
/// Input: nums = [3,4,-1,1]
892+
/// Output: 2
893+
/// Explanation: 1 is in the array but 2 is missing.
894+
///
895+
/// Example 3:
896+
///
897+
/// Input: nums = [7,8,9,11,12]
898+
/// Output: 1
899+
/// Explanation: The smallest positive integer 1 is missing.
900+
///
901+
/// Constraints:
902+
///
903+
/// 1 <= nums.length <= 10^5
904+
/// -2^31 <= nums[i] <= 2^31 - 1
905+
pub fn first_missing_positive(mut nums: Vec<i32>) -> i32 {
906+
for i in 0..nums.len() {
907+
if nums[i] == 0 {
908+
nums[i] = -1;
909+
}
910+
}
911+
912+
for i in 0..nums.len() {
913+
while nums[i] >= 1 && nums[i] <= nums.len() as i32 {
914+
let j = nums[i] as usize - 1;
915+
let tmp = nums[j];
916+
nums[j] = 0;
917+
if j == i {
918+
break;
919+
}
920+
if tmp != 0 {
921+
nums[i] = tmp;
922+
} else {
923+
nums[i] = -1;
924+
}
925+
}
926+
}
927+
928+
for (i, &num) in nums.iter().enumerate() {
929+
if num != 0 {
930+
return i as i32 + 1;
931+
}
932+
}
933+
nums.len() as i32 + 1
934+
}
876935
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /