287. Find the Duplicate Number Medium
1/**
2 * [287] Find the Duplicate Number
3 *
4 * Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
5 * There is only one repeated number in nums, return this repeated number.
6 * You must solve the problem without modifying the array nums and uses only constant extra space.
7 *
8 * Example 1:
9 *
10 * Input: nums = [1,3,4,2,2]
11 * Output: 2
12 *
13 * Example 2:
14 *
15 * Input: nums = [3,1,3,4,2]
16 * Output: 3
17 *
18 *
19 * Constraints:
20 *
21 * 1 <= n <= 10^5
22 * nums.length == n + 1
23 * 1 <= nums[i] <= n
24 * All the integers in nums appear only once except for precisely one integer which appears two or more times.
25 *
26 *
27 * Follow up:
28 *
29 * How can we prove that at least one duplicate number must exist in nums?
30 * Can you solve the problem in linear runtime complexity?
31 *
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/find-the-duplicate-number/
36// discuss: https://leetcode.com/problems/find-the-duplicate-number/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41 pub fn find_duplicate(nums: Vec<i32>) -> i32 {
42 0
43 }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 #[test]
53 fn test_287() {
54 }
55}
56
Back
© 2025 bowen.ge All Rights Reserved.