leetcode_middle_29_287. Find the Duplicate Number
题意:
有一个大小是n+1的数组。数组的元素都是[1, n]区间内的数字。必有且仅有一个数字重复(可能重复不止一次),找出这个数字
分析:
这个题的数字很有特色,数字都小于最大角标,就给了利用角标的机会。但怎么利用呢,画画想想之后突然想到是不是可以利用值做角标再用得到的值再做角标,这样必定有一个循环。因为这个循环,想到使用双指针,但是之后存在什么规律就没什么规律了。所以只好考虑另一种方法,(抽屉原理)因为本来是1 2 3 4 5 6 7...每个一个,但是这个题目的核心之处在于,比如2有两个(核心特征一般化),则必然可以推出这个数组不均匀了(左半比右半多了一个),所以可以确定这个数在多的那一边,所以二分查找:
public class Solution {public int findDuplicate(int[] nums) {int low = 1;int high = nums.length -1;int mid = 0;while(low<high) {mid = low + (high-low)/2;int c = com(nums, mid);if(c > mid) { //比如小于等于4的数有5个,那就应该查1到4high = mid;}else{ //比如小于等于4的数的个数是4个或者更少,应该查5到7low = mid+1;}}return low;}private int com(int[] nums, int mid) {int count = 0;for(int i=0; i < nums.length; i++){if(nums[i] <= mid)count++;}return count;}
}
leetcode_middle_29_287. Find the Duplicate Number相关推荐
- LeetCode 287. Find the Duplicate Number (时间复杂度O(n)) + 链表判断环
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
- LeetCode 287. Find the Duplicate Number
题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...
- LeetCode.287 Find the Duplicate Number
题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...
- LintCode Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- LeetCode 287---Find the Duplicate Number
问题链接:LeetCode 287-Find the Duplicate Number 题目大意 : 找出序列中唯一一个重复出现的数字,且只能使用o(1)的额外空间 实现代码如下: public cl ...
- 287. **Find the Duplicate Number
287. **Find the Duplicate Number https://leetcode.com/problems/find-the-duplicate-number/description ...
- D19:Duplicate Number(重复数字,翻译+题解)
原题:OpenJudge - 19:Duplicate Number 翻译: 描述:给定一个N个数的序列,求一个在序列中的至少出现2次的数A: 输入:第一行:一个不大于1000的正整数N : 第二行: ...
- leetcode 287. Find the Duplicate Number | 287. 寻找重复数(判断链表是否有环,并找到环的起点)
题目 https://leetcode.com/problems/find-the-duplicate-number/ 题解 题目有限制 不能修改数组元素,必须 O(1) 空间复杂度,所以 不能排序, ...
- 633 - Find the Duplicate Number
2017.9.11 感觉并没有啥意思啊这题,还是我误解了这题的真谛? public class Solution {/*** @param nums an array containing n + 1 ...
最新文章
- java接口作为参数_java-如何强制将通用类型参数作为接口?
- 【以太坊源码】交易(一)
- 白帽子讲Web安全(对看书之后的一点笔记)
- c语言调试的意义,C语言的调试
- python·文本分析
- 利用Android SAF(存储访问框架)进行游戏反和谐(伊甸园的骄傲)/Android data目录的访问限制
- java base64转图片打不开_解决通过 Base64 解码得到的图片无法打开查看的问题
- 个人Linux学习笔记操作大全
- Tableau:如何添加参考线、趋势线、参考区间、分布区间、盒须图?
- 部件MSCOMCTL.OCX或其附件之一不能正确注册:一个文件丢失或无效
- 利用ECharts可视化mysql数据库中的数据
- 微信{errcode:48001,errmsg:api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]}
- Office 顿号怎么输
- 如何提取pcap文件中的TCP流
- [FZYZOJ 2132] Zrn神犇之一起来看流星雨
- 计算机采用阿基米德,阿基米德与这个历时两千多年无人能解的牛群问题
- 商用图片素材,高清无水印
- 关于EditText输入数字密码
- 计算机课演示文稿模板,计算机网络拓扑结构公开课PPT模板课件
- python第五周项目答案_工作页python流程控制(第五周 ).doc_学小易找答案
热门文章
- 合计mysql php,php – mySQL中的条件求和?
- Win10 打印机共享问题解决实录
- BI是报表?BI是可视化?BI到底是什么?
- kissme病毒原理描述及python清除脚本
- C++STL标准库学习笔记(一)sort
- 移动互联网下半场争夺战:逃不过娱乐大网,得年轻中产得天下
- android联系人管理源码,计算机毕业课程设计源码-145安卓Android通讯录管理系统
- 持续集成/持续部署(1)Git Gitlab
- 微信小程序商城搭建,如何开发一个微信小程序商城
- 2020第一篇TED:Ideas worth spreading—— Debbie Millman: How symbols and brands shape our humanity?