-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst_missing_positive.rs
53 lines (49 loc) · 1.12 KB
/
first_missing_positive.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
///
/// Problem: First Missing Positive
///
/// Given an unsorted integer array `nums`, return the **smallest missing positive integer**.
///
/// **Example 1:**
/// ```plaintext
/// Input: nums = [1,2,0]
/// Output: 3
/// ```
///
/// **Example 2:**
/// ```plaintext
/// Input: nums = [3,4,-1,1]
/// Output: 2
/// ```
///
/// **Example 3:**
/// ```plaintext
/// Input: nums = [7,8,9,11,12]
/// Output: 1
/// ```
///
/// **Constraints:**
/// - `1 <= nums.length <= 10^5`
/// - `-2^31 <= nums[i] <= 2^31 - 1`
///
/// # Solution:
///
/// **Time Complexity:** `O(n)`
/// **Space Complexity:** `O(1)`
impl Solution {
pub fn first_missing_positive(nums: Vec<i32>) -> i32 {
let mut nums = nums;
let n = nums.len();
for i in 0..n {
while nums[i] > 0 && nums[i] <= n as i32 && nums[i] != nums[nums[i] as usize - 1] {
let target_index = nums[i] as usize - 1;
nums.swap(i, target_index);
}
}
for i in 0..n {
if nums[i] != (i + 1) as i32 {
return (i + 1) as i32;
}
}
(n + 1) as i32
}
}