-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path0041_first_missing_positive.rs
64 lines (56 loc) · 1.06 KB
/
0041_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
54
55
56
57
58
59
60
61
62
63
64
//! Given an unsorted integer array, find the smallest missing positive integer.
//!
//! ```
//! Example 1:
//!
//! Input: [1,2,0]
//! Output: 3
//! ```
//!
//! ```
//! Example 2:
//!
//! Input: [3,4,-1,1]
//! Output: 2
//! ```
//!
//! ```
//! Example 3:
//!
//! Input: [7,8,9,11,12]
//! Output: 1
//! ```
//!
//! Note:
//!
//! Your algorithm should run in O(n) time and uses constant extra space.
//!
struct Solution;
impl Solution {
pub fn first_missing_positive(mut nums: Vec<i32>) -> i32 {
let mut result = 1;
nums.sort();
nums.iter().for_each(|num| {
if *num == result {
result += 1;
}
});
result
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn test_0() {
assert_eq!(Solution::first_missing_positive(vec![1, 2, 0]), 3);
}
#[test]
fn test_1() {
assert_eq!(Solution::first_missing_positive(vec![3, 4, -1, 1]), 2);
}
#[test]
fn test_2() {
assert_eq!(Solution::first_missing_positive(vec![7, 8, 9, 11, 12]), 1);
}
}