feature: mid-maximum-subarray

This commit is contained in:
daniel.w 2024-07-16 13:52:00 +08:00
parent 7b966bd4ba
commit 6ea7008734
1 changed files with 20 additions and 3 deletions

View File

@ -1,8 +1,25 @@
fn main() { fn main() {
println!("Hello, world!"); let mut case_a: Vec<i32> = vec![3, 2, 2, 3];
// 傳入時也要帖別聲明 &mut -> &mut case_a
let result = remove_duplicates(&mut case_a, 3);
println!("Result: {}", result);
} }
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 { pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
let mut slow_pointer :usize= 0;
let mut fast_pointer:usize = 0; let mut fast_pointer:usize = 0;
let mut slow_pointer:usize = 0;
let stop = nums.len();
let mut target = nums[0];
// [), 想停止條件
while fast_pointer < stop {
if nums[fast_pointer] != target {
nums[slow_pointer] = nums[fast_pointer];
slow_pointer += 1;
target = nums[fast_pointer]
}
fast_pointer +=1;
}
return slow_pointer as i32
} }