rs: add «41. First Missing Positive»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
48d437c878
commit
5c3727b0d9
1 changed files with 21 additions and 0 deletions
21
rs/first-missing-positive.rs
Normal file
21
rs/first-missing-positive.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue