Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode 0287 - Find the Duplicate Number

LeetCode 0287 - Find the Duplicate Number

作者头像
Reck Zhang
发布于 2021-08-11 03:51:39
发布于 2021-08-11 03:51:39
31200
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

Find the Duplicate Number

Desicription

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Example 1:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: [1,3,4,2,2]
Output: 2

Example 2:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: [3,1,3,4,2]
Output: 3

Note:

  1. You must not modify the array (assume the array is read only).
  2. You must use only constant, O(1) extra space.
  3. Your runtime complexity should be less than O(n^2).
  4. There is only one duplicate number in the array, but it could be repeated more than once.

Solution

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        if(nums.size() <= 1) {
            return 0;
        }
        int slow = nums[0];
        int first = nums[nums[0]];
        while(slow != first) {
            slow = nums[slow];
            first = nums[nums[first]];
        }
        first = 0;
        while(first != slow) {
            slow = nums[slow];
            first = nums[first];
        }
        return slow;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Leetcode 287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You must not modify the
triplebee
2018/01/12
6610
重中之重的二分查找
There are two sorted arrays nums1 and nums2 of size m and n respectively.
王脸小
2020/07/28
4920
LeetCode分类刷题:双指针(Two Pointers)
双指针(Two Pointers)一直是程序员面试中的一个必须准备的主题, 面试中双指针出现的次数比较多,主要由于在工作中指针经常用到,指针问题能够直接反应面试者的基础知识、代码能力和思维逻辑,因此双指针的问题必须掌握。
ACM算法日常
2019/03/06
2K0
LeetCode 287. 寻找重复数(BitMap)
给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。
Michael阿明
2021/02/20
5300
LeetCode 287. 寻找重复数(BitMap)
LeetCode 题目解答——第 227 到 310 题
[Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复杂度上根本就不是最优解,有的写的太啰嗦,有的则用了一些过于 tricky 的方法。我没有为了这个再更新,就让它们去吧。
四火
2022/07/19
1.2K0
LeetCode 题目解答——第 227 到 310 题
LeetCode 0137 - Single Number II
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Reck Zhang
2021/08/11
1970
【LeetCode】448. Find All Numbers Disappeared in an Array
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
韩旭051
2019/11/07
3370
LeetCode 0219 - Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
Reck Zhang
2021/08/11
2660
LeetCode 0220 - Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
Reck Zhang
2021/08/11
2980
关关的刷题日记03—Leetcode 448. Find All Numbers Disappeared in an Array
题目 448. Find All Numbers Disappeared in an Array Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Could you do
WZEARW
2018/04/08
6610
LeetCode笔记:448. Find All Numbers Disappeared in an Array
转换一个思路,我们先遍历一遍数组,因为元素内容都在1~数组长度中,所以我们将元素值对应位置值都设为负数,比如例子中我们将第四个元素、第三个元素、第二个元素等等都设为负数(这里注意元素值要减一才是位置),当然无论一个元素出现几次,我们都保证对应位置的值设为负数。
Cloudox
2021/11/23
3000
LeetCode442. Find All Duplicates in an Array解题
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
vincentbbli
2021/08/18
3080
448. Find All Numbers Disappeared in an Array(找到所有数组中消失的数字)
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
砖业洋__
2023/05/06
1560
448. Find All Numbers Disappeared in an Array(找到所有数组中消失的数字)
LeetCode 0136 - Single Number
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Reck Zhang
2021/08/11
2980
leetcode-448-Find All Numbers Disappeared in an Array
题目描述: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice
chenjx85
2018/05/22
4570
单调栈-LeetCode 739、287(单调栈,桶计数)
根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数。如果之后都不会升高,请在该位置用 0 来代替。 例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]。
算法工程师之路
2019/11/14
6350
LeetCode 0217 - Contains Duplicate
Given an array of integers, find if the array contains any duplicates.
Reck Zhang
2021/08/11
3980
【LeetCode】136. Single Number
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
韩旭051
2019/11/07
4130
Find the Duplicate Number
1. Description 2. Solution Version 1 class Solution { public: int findDuplicate(vector<int>& num
Tyan
2019/05/25
4330
每日算法系列【LeetCode 287】寻找重复数
给定一个包含 个整数的数组 ,其数字都在 到 之间(包括 和 ),可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。
godweiyang
2020/03/24
7100
每日算法系列【LeetCode 287】寻找重复数
相关推荐
Leetcode 287. Find the Duplicate Number
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验