diff --git a/array/fast_slow_pointer_26/src/main.rs b/array/fast_slow_pointer_26/src/main.rs index b2eb06c..bf83344 100644 --- a/array/fast_slow_pointer_26/src/main.rs +++ b/array/fast_slow_pointer_26/src/main.rs @@ -1,8 +1,25 @@ fn main() { - println!("Hello, world!"); + let mut case_a: Vec = 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 { - 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 } \ No newline at end of file