Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create 0287-find-the-duplicate-number.rs #2627

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
create 0287-find-the-duplicate-number.rs
  • Loading branch information
rmrt1n committed Jun 23, 2023
commit 7264d28fdf689abbf45c3eb74d1051c3e3994fed
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
}
}