rust_test/src/main.rs

33 lines
887 B
Rust
Raw Normal View History

struct Solution;
impl Solution {
2025-09-06 16:56:09 +08:00
pub fn first_missing_positive(mut nums: Vec<i32>) -> i32 {
for i in 0..nums.len(){
if nums[i] <= 0 || nums[i] > nums.len() as i32 {
continue;
}
let mut temp = nums[i];
nums[i] = -1;
while temp > 0 && temp <= nums.len() as i32 {
let temp2 = nums[temp as usize - 1];
nums[temp as usize - 1] = temp;
if temp == temp2 {
break;
}
temp = temp2;
}
}
2025-09-06 16:56:09 +08:00
println!("{:?}", nums);
for i in 0..nums.len(){
if nums[i] != (i as i32 + 1) {
return i as i32 + 1;
}
}
2025-09-06 16:56:09 +08:00
nums.len() as i32 + 1
}
}
fn main() {
2025-09-06 16:56:09 +08:00
let sl = Solution::first_missing_positive(vec![1]);
println!("{:?}", sl);
}