Find the Duplicate Number
来源:互联网 发布:mysql innerjoin 编辑:程序博客网 时间:2024/06/07 06:05
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 array (assume the array is read only).
- You must use only constant, O(1) extra space.
- Your runtime complexity should be less than
O(n2)
. - There is only one duplicate number in the array, but it could be repeated more than once.
这道题考查的是二分搜索。还是参考了网上的思路。感觉还是比较有难度的题。
代码:
public int findDuplicate(int[] nums) { if(nums == null || nums.length == 0) return 0; int low = 1; int high = nums.length-1;// 0~n n个数字,nums.length = n + 1 while(low<high){ int mid = low + (high - low)/2; int count = 0; for(int a : nums){ if(a<=mid){ count++; } } if(count <=mid){ low = mid +1; }else{ high = mid; } } return low; }
0 0
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find The Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- 网络图片查看器
- Arrays的asList详解
- 16位汇编第四讲----8086寻找方式
- POJ 3630 Phone List 已被翻译
- 动态内存分配
- Find the Duplicate Number
- C语言:静态顺序表的实现和相关操作
- Linux细说
- 比较IC卡、ID卡、M1卡、CPU卡它们之间有什么区别?
- 325. Maximum Size Subarray Sum Equals k
- 排序
- 使用nginx 隔离进行简单的拦截
- 微信服务号服务器配置和企业号回调接口配置的区别
- JavaScript实现表格的添加和删除