Skip to content

Commit

Permalink
create 0287-find-the-duplicate-number.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrt1n committed Jun 23, 2023
1 parent 17804ed commit 7264d28
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions rust/0287-find-the-duplicate-number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
impl Solution {
pub fn find_duplicate(nums: Vec<i32>) -> i32 {
let (mut slow, mut fast) = (nums[0], nums[nums[0] as usize]);
while slow != fast {
slow = nums[slow as usize];
fast = nums[nums[fast as usize] as usize];
}

slow = 0;
while slow != fast {
slow = nums[slow as usize];
fast = nums[fast as usize];
}

slow
}
}

0 comments on commit 7264d28

Please sign in to comment.