287. Find the Duplicate Number [Leetcode]
来源:互联网 发布:淘宝搞笑收件人名字女 编辑:程序博客网 时间:2024/06/07 20:30
这题首先想到的最简单的方法是用快排然后再遍历。
然而题目要求不能改变原来的数组顺序。
所以快排的方法被排除。
后来想到了用binary search来做。
方法一:Binary Search
由于数字的范围是确定的[1,n]
取数字mid,如果【1,mid)没有重复的数字的话,这区间的数字个数应该<= mid - 1,反之则 > mid - 1, 如果在(mid,n] 之间没有重复数字的话,该区间的数字个数应该<= n - mid - 1,反之则 > (mid,n]. 若两个区间内的数字均未超量,则说明mid就是重复的数字。
该方法时间复杂度为O(NlogN)
代码如下:
class Solution {public: int findDuplicate(vector<int>& nums) { int left = 1, right = nums.size() - 1; while (left < right) { int mid = (left + right) / 2; int smaller = 0, bigger = 0; for (auto i : nums) { if (i < mid) smaller++; if (i > mid) bigger++; } if (smaller <= mid - 1 && bigger <= nums.size() - 1 -mid) return mid; else if (smaller > mid - 1) right = mid - 1; else left = mid + 1; } return (left + right) / 2; }};
方法二:Floyd Cycle Detection
看评论区大神用的方法,很妙,不过一开始真的挺难理解的,因为没做过 Linked List Cycle II 这道题,只有弄明白了Linked List Cycle II ,才有办法理解。
这种方法是把每个数字的值看作是指向的下一个数字的下标。由于数字范围在【1,n】,数组下标范围在【0,n】,所以不用担心访问时的下标越界问题。
如数组【1,4,2,3,3】
对一个下标:【0,1,2,3,4】
比如,从第一个数字1出发,它指向的是位置为1的数字4,数字4指向位置为4的数字3....... 这样下去,我们可以构成一个带回路的数字链。
why?为什么一定会出现回路?
我是这样推理的:
由于指向的数组下标为【1,n】,对于任何一个数字,永远存在下一个指向的数字。无论从任何一个数字开始出发的数字链都不存在一个所谓的“终点”,可想而知,一定会有环的存在。
明白了这点后,问题就变成了求链中回路的起点。
如何求回路的起点?
在这里,我们设置两个pointer, 一个为fast,一次走两步,一个为slow,一次走一步。
假设两个pointer在回路地点开始后的第K个节点相遇。回路的长度为Y,链的起点到回路的起点距离为X。
我们可以得出以下公式:
对于 pointer slow: t = X+nY+K
对于 pointer fast: 2t = X+mY+K (m,n is unknown)
From above equation, we could get:
2X + 2nY + 2K = X + mY + K
=> X+K = (m-2n)Y
因此当他们第一次相遇后,只要将fast指针再放到链条起点处,然后两指针同时一步步移动,当他们再次相遇时,一定就是在回路的起点处。
该方法时间复杂度为 O(N)
以下是leetcode上通过的代码:
class Solution {public: int findDuplicate(vector<int>& nums) { int fast = nums[nums[0]], slow = nums[0]; while (fast != slow){ fast = nums[nums[fast]]; slow = nums[slow]; } fast = 0; while (fast != slow){ fast = nums[fast]; slow = nums[slow]; } return fast; }};
方法二:位操作
想出这个办法的人也是非常厉害了。
时间复杂度为O(32N)
设一个长度为32的循环,第i次循环里:
设变量a,为[1,2,3,...N]数字集在第i为(二进制)的和, b为nums数字集在第i位的和。若 b > a,则说明重复的数字在该位上为1.
代码下:
class Solution {public: int findDuplicate(vector<int>& nums) { int res = 0; for (int i = 0; i < 31; i++) { int bit = 1 << i; int a = 0, b = 0; for (int j = 0; j < nums.size(); j++) { if (j > 0) a += j & bit; b += nums[j] & bit; } if (b > a) res += bit; } return res; }};
- [leetcode] 287.Find the Duplicate Number
- [leetcode] 287. Find the Duplicate Number
- <LeetCode OJ> 287. Find the Duplicate Number
- Leetcode 287. Find the Duplicate Number
- Leetcode - 287. Find the Duplicate Number
- [LeetCode]287. Find the Duplicate Number
- LeetCode 287. Find the Duplicate Number
- 【leetcode】287. Find the Duplicate Number
- 【leetcode】287. Find the Duplicate Number
- leetcode 287. Find the Duplicate Number
- LCP287 LeetCode 287. Find the Duplicate Number
- [Leetcode]287. Find the Duplicate Number
- Leetcode题解 287. Find the Duplicate Number
- [LEETCODE] 287. Find the Duplicate Number
- [LeetCode] 287. Find the Duplicate Number
- <leetcode>287. Find the Duplicate Number
- [leetcode]287.Find the Duplicate Number
- LeetCode 287. Find the Duplicate Number
- 阿里云助力网络安全社区MMBEST,书写云上新面貌
- canvas里面刷新界面,有时候图片能出来 有时候不能
- 欢迎使用CSDN-markdown编辑器
- 程序或-内存区域分配(五个段)--终于搞明白了
- watch的使用
- 287. Find the Duplicate Number [Leetcode]
- 实习项目一总结
- 哈密顿绕行世界问题
- 因子个数打表
- Vue用v-for给src属性赋值
- android用jxl读取Excel(2003,2007)
- 如何使用git提交代码到github主页上(基础)
- hdu 1556 Color the ball(线段树区间更新单点查询+懒惰标记)
- 深度学习第三章-概率与信息论笔记