1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00
LeetCode/rs/first-missing-positive.rs
Matej Focko 5c3727b0d9
rs: add «41. First Missing Positive»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-03-26 22:08:37 +01:00

21 lines
516 B
Rust

impl Solution {
pub fn first_missing_positive(nums: Vec<i32>) -> i32 {
let n = nums.len() + 1;
let mut seen = vec![false; n];
for x in nums.into_iter() {
if x <= 0 || (n as i32) <= x {
continue;
}
seen[x as usize - 1] = true;
}
1 + seen
.into_iter()
.enumerate()
.find(|(_, val)| !val)
.expect("there's always a missing positive integer")
.0 as i32
}
}