MCPcopy Create free account
hub / github.com/Ainevsia/Leetcode-Rust / first_missing_positive

Method first_missing_positive

41. First Missing Positive/src/main.rs:8–23  ·  view source on GitHub ↗
(mut nums: Vec<i32>)

Source from the content-addressed store, hash-verified

6
7impl Solution {
8 pub fn first_missing_positive(mut nums: Vec<i32>) -> i32 {
9 nums = nums.into_iter().filter(|&x| x > 0).collect();
10 if nums.is_empty() { return 1 }
11 let k = nums.len();
12 // ret should within [1..k+1]
13 for i in 0..k {
14 let n = i32::abs(nums[i]) as usize;
15 if n >= k + 1 { continue }
16 // 1<=n<=k k numbers
17 if nums[n-1] > 0 { nums[n-1] = - nums[n-1] }
18 }
19 for i in 0..k {
20 if nums[i] > 0 { return (i + 1) as i32 }
21 }
22 (k + 1) as i32
23 }
24}
25
26#[cfg(test)]

Callers

nothing calls this directly

Calls 1

into_iterMethod · 0.80

Tested by

no test coverage detected