0287.寻找重复数
方法一:快慢指针
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
impl Solution {
pub fn find_duplicate(nums: Vec<i32>) -> i32 {
let mut slow = nums[0] as usize;
let mut fast = nums[nums[0] as usize] as usize;
while slow != fast {
slow = nums[slow] as usize;
fast = nums[nums[fast] as usize] as usize;
}
let mut slow = 0 as usize;
let mut fast = fast as usize;
while slow != fast {
slow = nums[slow] as usize;
fast = nums[fast] as usize;
}
slow as i32
}
}