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.

注意事项

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

样例
Given nums = [5,5,4,3,2,1] return 5
Given nums = [5,4,4,3,2,1] return 4

the First Solution:忽视掉注意事项,把数组排序,然后比较相邻元素,相同返回即可,在LintCode上也可以AC。
代码如下:

public class Solution {/** @param nums: an array containing n + 1 integers which is between 1 and n* @return: the duplicate one*/public int findDuplicate(int[] nums) {// write your code hereArrays.sort(nums);for(int i=0;i<nums.length-1;i++){if(nums[i]==nums[i+1]){return nums[i];}}return nums[0];}
}

the Second Solution:二分法。对于1到n的整数,例如1到10的整数,小于等于5的数,最少有5个(1,2,3,4,5),如果多于5个,那么一定是多了一个重复的数在1到5之间,那么下次就在1到5中间找,否则在6到10里面找(注意这里的二分法,不是对数组中的元素进行二分,而是对于1到n这n个数字进行二分查找)。
代码如下:

public class Solution {/** @param nums: an array containing n + 1 integers which is between 1 and n* @return: the duplicate one*/public int findDuplicate(int[] nums) {// write your code hereint l=1,r=nums.length-1,mid;while(l<r){mid=l+(r-l)/2;int count=0;for(int x:nums){if(x<=mid){count++;}}if(count>mid){r=mid;}else{l=mid+1;}}return l;}
}

the Third Solution: 参考资料
环检测问题,参考了上面链接的资料(有点没怎么理解,不过简单一点来说就是下面这样的),对于数组中的元素,对其做一个映射({1….n}到{1…n}的映射),f(nums[i])=A[ nums[i] ](i是数组下标),这样映射之后,相当于有了一个新的链表(我只是这么说,并没有构造一个链表),而且这个链表是带环的,因为有一个重复元素的存在。例如下面这个例子:
原数组nums: 5,4,3,4,2,1.
映射之后新的链表: 5,1,4,2,3,4,2,3,4,2,3………………其中4,2,3,4就形成了一个环。
那么问题到这,就可以参考之前写过的一篇文章,也是LintCode上另一道题,带环链表,就可以得到这道题的解法。
代码如下:

public class Solution {/** @param nums: an array containing n + 1 integers which is between 1 and n* @return: the duplicate one*/public int findDuplicate(int[] nums) {// write your code hereint slow=0,fast=0;while(true){slow=nums[slow];fast=nums[nums[fast]];if(fast==slow){break;}}int p=0;while(true){slow=nums[slow];p=nums[p];if(slow==p){break;}}return p;}
}

LintCode Find the Duplicate Number相关推荐

  1. LeetCode 287. Find the Duplicate Number (时间复杂度O(n)) + 链表判断环

    LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...

  2. LeetCode 287. Find the Duplicate Number

    题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...

  3. LeetCode.287 Find the Duplicate Number

    题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...

  4. LeetCode 287---Find the Duplicate Number

    问题链接:LeetCode 287-Find the Duplicate Number 题目大意 : 找出序列中唯一一个重复出现的数字,且只能使用o(1)的额外空间 实现代码如下: public cl ...

  5. 287. **Find the Duplicate Number

    287. **Find the Duplicate Number https://leetcode.com/problems/find-the-duplicate-number/description ...

  6. D19:Duplicate Number(重复数字,翻译+题解)

    原题:OpenJudge - 19:Duplicate Number 翻译: 描述:给定一个N个数的序列,求一个在序列中的至少出现2次的数A: 输入:第一行:一个不大于1000的正整数N : 第二行: ...

  7. 【Lintcode】046.Majority Number

    题目: Given an array of integers, the majority number is the number that occurs more than half of the ...

  8. leetcode 287. Find the Duplicate Number | 287. 寻找重复数(判断链表是否有环,并找到环的起点)

    题目 https://leetcode.com/problems/find-the-duplicate-number/ 题解 题目有限制 不能修改数组元素,必须 O(1) 空间复杂度,所以 不能排序, ...

  9. 633 - Find the Duplicate Number

    2017.9.11 感觉并没有啥意思啊这题,还是我误解了这题的真谛? public class Solution {/*** @param nums an array containing n + 1 ...

最新文章

  1. 计算机网络及应用 pdf,计算机网络及应用卷.pdf
  2. vuex页面数据丢失_使用vuex解决刷新页面state数据消失的问题记录
  3. oracle 保留一个记录吗,笔记:Oracle查询重复数据并删除,只保留一条记录
  4. @echo off是什么意思_为什么执行自己的程序要在前面加./
  5. ubuntu gedit出错:Failed to connect to the session manager
  6. Java虚拟机JVM的内部体系结构
  7. c语言long double位数,int long double 所占位数 和最大值
  8. pandas 空字符串与na区别_python从安装到数据分析应用高手 Pandas处理文本数据(一)...
  9. ubuntu emacs的安装
  10. JNDI学习总结(2)——JNDI数据源的配置
  11. 30个超实用Python代码片段
  12. HDU 6336 Matrix from Arrays (杭电多校4E)
  13. ZooKeeper学习第八期——ZooKeeper伸缩性
  14. 无比强悍的CRM营销模块,SuiteCRM功能介绍
  15. ArcGIS操作系列(一)之地理配准
  16. 解决在win2003 enterprise上搭建IIS遇到的“需要Service Pack 2 CD-Rom 上的文件“问题
  17. 2021年日本互联网服务业发展现状:ZHD和LINE 的合并将改变日本互联网行业格局[图]
  18. Mac系统下如何创建锁屏快捷键
  19. 《哲学史讲演录》——思辨节选---塞诺芬尼 、巴门尼德
  20. linux终端分辨率对应表,Linux下设置终端分辨率,最全的VGA代码和分辨率对照表...

热门文章

  1. python bind_Python tkinter之Bind(绑定事件)
  2. 1月31日 解决问题的方法( 麦肯锡七步成诗法 )
  3. 初次模拟小鲨鱼记账本1.0
  4. iOS JS与OC交互
  5. CMT2380/HC32L110入门踩坑记录
  6. 盟百照相馆影楼摄影工作室选片系统
  7. A002-185-2510-李海鹏
  8. Minecraft 1.12.2模组开发(一) 配置ForgeMDK环境
  9. C++内存管理方式——new/delete
  10. 以“人民为中心”重新定义全球新型智慧城市, 2021-2022年中国新型智慧城市百强榜权威发布!