https://leetcode.com/problems/find-all-duplicates-in-an-array/description/

参考:http://www.cnblogs.com/grandyang/p/4843654.html

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements that appear twice in this array.

Could you do it without extra space and in O(n) runtime?

Example:

Input:
[4,3,2,7,8,2,3,1] Output:
[2,3]

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

Note:

  1. You must not modify the array (assume the array is read only).
  2. You must use only constant extra space.
  3. Your runtime complexity should be less than O(n2).

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

这道题给了我们n+1个数,所有的数都在[1, n]区域内,首先让我们证明必定会有一个重复数,这不禁让我想起了小学华罗庚奥数中的抽屉原理(又叫鸽巢原理), 即如果有十个苹果放到九个抽屉里,如果苹果全在抽屉里,则至少有一个抽屉里有两个苹果,这里就不证明了,直接来做题吧。题目要求我们不能改变原数组,即不能给原数组排序,又不能用多余空间,那么哈希表神马的也就不用考虑了,又说时间小于O(n2),也就不能用brute
force的方法,那我们也就只能考虑用二分搜索法了,我们在区别[1,
n]中搜索,首先求出中点mid,然后遍历整个数组,统计所有小于等于mid的数的个数,如果个数大于mid,则说明重复值在[mid+1,
n]之间,反之,重复值应在[1, mid-1]之间,然后依次类推,直到搜索完成,此时的low就是我们要求的重复值,参见代码如下:

解法一:

class Solution {
public:
int findDuplicate(vector<int>& nums) {
int low = 1, high = nums.size() - 1;
while (low < high) {
int mid = low + (high - low) * 0.5;
int cnt = 0;
for (auto a : nums) {
if (a <= mid) ++cnt;
}
if (cnt <= mid) low = mid + 1;
else high = mid;
}
return low;
}
};

经过热心网友waruzhi的留言提醒还有一种O(n)的解法,并给了参考帖子,发现真是一种不错的解法,其核心思想快慢指针在之前的题目Linked List Cycle II中就有应用,这里应用的更加巧妙一些,由于题目限定了区间[1,n],所以可以巧妙的利用坐标和数值之间相互转换,而由于重复数字的存在,那么一定会形成环,我们用快慢指针可以找到环并确定环的起始位置,确实是太巧妙了!

解法二:

class Solution {
public:
int findDuplicate(vector<int>& nums) {
int slow = , fast = , t = ;
while (true) {
slow = nums[slow];
fast = nums[nums[fast]];
if (slow == fast) break;
}
while (true) {
slow = nums[slow];
t = nums[t];
if (slow == t) break;
}
return slow;
}
};

类似题目:

First Missing Positive

Missing Number

Find All Numbers Disappeared in an Array

Set Mismatch

Array Nesting

参考资料:

https://leetcode.com/discuss/60830/python-solution-explanation-without-changing-input-array

https://discuss.leetcode.com/topic/29101/simple-c-code-with-o-1-space-and-o-nlogn-time-complexity

https://discuss.leetcode.com/topic/25913/my-easy-understood-solution-with-o-n-time-and-o-1-space-without-modifying-the-array-with-clear-explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

分类: LeetCode

leetcode 442. Find All Duplicates in an Array 查找数组中的所有重复项的更多相关文章

  1. Leetcode#442. Find All Duplicates in an nums(数组中重复的数据)

    题目描述 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O(n)时间复杂度内解 ...

  2. LeetCode 442. 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 ...

  3. LeetCode - 442. Find All Duplicates in an Array - 几种不同思路 - (C++)

    题目 题目链接 Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ...

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  5. [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  6. leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array

    后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...

  7. 442. Find All Duplicates in an Array - LeetCode

    Question 442. Find All Duplicates in an Array Solution 题目大意:在数据中找重复两次的数 思路:数组排序,前一个与后一个相同的即为要找的数 Jav ...

  8. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  9. 乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array

    乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array 一.前言     我们这次的实验是去除重复的有序数组元素,有大体两种算法. 二.Remo ...

随机推荐

  1. Oracle查询优化改写--------------------给查询结果排序

    一.查看员工所雇员工信息(查询部门号==10并且按照入职时间升序排序.第二种用数字来代替) 二.按多个字段排序(dmpno,deptno,sal,ename,job) 三.按照子串排序(有一种速查方法 ...

  2. 网络工具nslookup的使用

    根据域名查询ip 如下所示: bogon:~ hhh$ nslookup www.baidu.com. Server: 192.168.1.254. #默认的DNS服务器 Address: . #ip ...

  3. ZJOI2018游记

    我是一只普及组的菜鸡,我很菜 我参加 \(ZJOI\) 只是来试试水(水好深啊~),看看大佬(差距好大啊~),以后要好好学习 \(day0\) 下午2:00,颁奖 还以为要到很晚,还是挺快的 \(da ...

  4. 记录python接口自动化测试--requests使用和基本方法封装(第一目)

    之前学习了使用jmeter+ant做接口测试,并实现了接口的批量维护管理(大概500多条用例),对"接口"以及"接口测试"有了一个基础了解,最近找了一些用pyt ...

  5. Beta版本敏捷冲刺每日报告——Day1

    1.情况简述 Beta阶段第一次Scrum Meeting 敏捷开发起止时间 2017.11.2 08:00 -- 2017.11.2 21:00 讨论时间地点 2017.11.2晚6:00,软工所实 ...

  6. DML数据操作语言之复杂查询

    1.视图(View) 我们知道,在关系型数据库中,用来保存实际数据记录的是数据表.和表同等概念也是用来保存东西是:视图. 但是数据表是用来保存实际数据记录的,而视图是用来保存常用select语句的. ...

  7. [JCIP笔记] (三)如何设计一个线程安全的对象

    在当我们谈论线程安全时,我们在谈论什么中,我们讨论了怎样通过Java的synchronize机制去避免几个线程同时访问一个变量时发生问题.忧国忧民的Brian Goetz大神在多年的开发过程中,也悟到 ...

  8. Windows中添加自己的程序到开机启动中(添加服务,添加注册表)

    在系统启动的时候启动自己想要启动的程序: 方法一:利用开机启动文件夹 将exe文件或exe文件的快捷方式复制到(启动)文件夹下 以win7为例:开始→所有程序→启动→鼠标右键打开 方法二:添加系统服务 ...

  9. 第一次制作和使用图标字体-IcoMoon

    开题:之前就有所耳闻,最近两天第一次运用到图标字体.刚开始嘛,一脸懵逼的状态.成功运用之后就来记录一下使用过程咯! 1. 打开在线生成工具:https://icomoon.io/app/#/selec ...

  10. android 运行时异常捕获

    1,将运行时异常捕获并存到手机SD卡上 可以直接使用logcat 命令Runtime.getRuntime().exec("logcat -f "+ file.getAbsolut ...