LeetCode //C - 287. Find the Duplicate Number

287. Find the Duplicate Number

Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.

There is only one repeated number in nums, return this repeated number.

You must solve the problem without modifying the array nums and uses only constant extra space.
 

Example 1:

Input: nums = [1,3,4,2,2]
Output: 2

Example 2:

Input: nums = [3,1,3,4,2]
Output: 3

Example 3:

Input: nums = [3,3,3,3,3]
Output: 3

Constraints:
  • 1 < = n < = 1 0 5 1 <= n <= 10^5 1<=n<=105
  • nums.length == n + 1
  • 1 <= nums[i] <= n
  • All the integers in nums appear only once except for precisely one integer which appears two or more times.

From: LeetCode
Link: 287. Find the Duplicate Number


Solution:

Ideas:
  1. Initialization: Both the tortoise and the hare start at the first element (index 0).

  2. Phase 1 - Detecting the cycle:

  • The tortoise moves one step at a time (nums[tortoise]), while the hare moves two steps at a time (nums[nums[hare]]).
  • If there’s a cycle (which there is, due to the duplicate), the hare will eventually meet the tortoise within this cycle, proving the cycle’s existence.
  1. Phase 2 - Locating the cycle’s start:
  • After the meeting, reset one of the runners to the start of the list (index 0), keeping the other at the meeting point.
  • Move both at the same speed (one step at a time). The point at which they meet again is the start of the cycle, which corresponds to the duplicate number in the array.
Code:
int findDuplicate(int* nums, int numsSize) {
    // Phase 1: Finding the intersection point of the two runners.
    int tortoise = nums[0];
    int hare = nums[0];
    do {
        tortoise = nums[tortoise];
        hare = nums[nums[hare]];
    } while (tortoise != hare);

    // Phase 2: Find the "entrance" to the cycle.
    tortoise = nums[0];
    while (tortoise != hare) {
        tortoise = nums[tortoise];
        hare = nums[hare];
    }

    return hare;
}

相关推荐

  1. Leetcode 287. 寻找重复数

    2024-03-26 08:22:01       45 阅读
  2. leetcode——287 寻找重复数

    2024-03-26 08:22:01       46 阅读
  3. LeetCode 287 寻找重复数字

    2024-03-26 08:22:01       35 阅读
  4. 207. 课程表

    2024-03-26 08:22:01       64 阅读
  5. 面试经典150题(27-28)

    2024-03-26 08:22:01       57 阅读
  6. LeetCode //C - 287. Find the Duplicate Number

    2024-03-26 08:22:01       45 阅读
  7. 207. Course Schedule

    2024-03-26 08:22:01       71 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-26 08:22:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-26 08:22:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-26 08:22:01       87 阅读
  4. Python语言-面向对象

    2024-03-26 08:22:01       96 阅读

热门阅读

  1. 连接 IPv6 服务器

    2024-03-26 08:22:01       44 阅读
  2. Git 入门教程

    2024-03-26 08:22:01       48 阅读
  3. 机器学习中的数学原理——模型评估与交叉验证

    2024-03-26 08:22:01       48 阅读
  4. 零基础入门多媒体音频(5)-alsa(1)

    2024-03-26 08:22:01       41 阅读
  5. 【报错】note: module requires Go 1.18

    2024-03-26 08:22:01       33 阅读
  6. minio静态html不显示js样式

    2024-03-26 08:22:01       39 阅读
  7. NPM是什么?及常用的命令

    2024-03-26 08:22:01       39 阅读
  8. ARMday5

    2024-03-26 08:22:01       45 阅读
  9. 开发npm上传发布

    2024-03-26 08:22:01       40 阅读
  10. 6、jenkins项目构建类型1-项目类型介绍

    2024-03-26 08:22:01       35 阅读