Find the duplicate Number (鸽巢原理) leetcode java
问题描述:
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.
分析:有n + 1数,其中每个数的范围都是1-n,可以证明这n+1个数中至少有两个数是相同的(鸽巢原理)。假设正好只有两个数是相同的,请找出重复出现的数。
方法一:快速排序,时间复杂度O(nlogn)
public int findDuplicate(int[] nums) {
int n = nums.length; //长度
quickSort(nums,0,n - 1); //快速排序,时间复杂度小于O(n*n)
for(int i = 0; i < n - 1; i++){
if(nums[i] == nums[i + 1] )
return nums[i];
}
return -1;
}
//快速排序
public static void quickSort(int[] nums,int left,int right){
int dp;
if (left < right) {
dp = partition(nums, left, right);
quickSort(nums, left, dp - 1);
quickSort(nums, dp + 1, right);
}
}
//划分
public static int partition(int n[], int left, int right) {
int pivot = n[left];
while (left < right) {
while (left < right && n[right] >= pivot)
right--;
if (left < right)
n[left++] = n[right];
while (left < right && n[left] <= pivot)
left++;
if (left < right)
n[right--] = n[left];
}
n[left] = pivot;
return left;
}
方法二:二分查找
//二分查找 :若小于mid的数有mid个,则在upperPart查找,否则,在lowerPart查找
public static int findDuplicate(int[] nums){
int n = nums.length - 1; // 总共 n+1 个元素
int low = 1; //数组中可能的最小值
int high = n; //数组中可能的最大值
int mid = 0;
while(low < high){
mid = low + (high - low) / 2; //取中间值
int c = count(mid, nums); //统计小于mid的元素
if(c <= mid){//重复值应该在upperPart
low = mid + 1;
}else { //重复值应该在lowerPart
high = mid - 1;
}
}
return low;
} public static int count(int mid,int[] nums){
int c = 0;
for (int i = 0; i < nums.length; i++) {
if(nums[i] <= mid)
c++;
}
return c;
}
Find the duplicate Number (鸽巢原理) leetcode java的更多相关文章
- HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- cf319.B. Modulo Sum(dp && 鸽巢原理 && 同余模)
B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- poj 2356 Find a multiple(鸽巢原理)
Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...
- [POJ3370]&[HDU1808]Halloween treats 题解(鸽巢原理)
[POJ3370]&[HDU1808]Halloween treats Description -Every year there is the same problem at Hallowe ...
- [POJ2356]Find a multiple 题解(鸽巢原理)
[POJ2356]Find a multiple Description -The input contains N natural (i.e. positive integer) numbers ( ...
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) F. Double Knapsack 鸽巢原理 构造
F. Double Knapsack 题目连接: http://www.codeforces.com/contest/618/problem/F Description You are given t ...
- [POJ2356] Find a multiple 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8776 Accepted: 3791 ...
随机推荐
- #pragma data_seg() 共享数据// MyData段 // 进程 // DLL
https://www.cnblogs.com/dongsheng/p/4476157.html http://www.cnblogs.com/CBDoctor/archive/2013/01/26/ ...
- sql server查看用户权限
System.ServiceModel.FaultException: Server error. Detail: The EXECUTE permission was denied on the o ...
- hihoCoder week1 最长回文子串
题目链接 https://hihocoder.com/contest/hiho1/problem/1 做法 Manacher #include <bits/stdc++.h> using ...
- (转)ResNet, AlexNet, VGG, Inception: Understanding various architectures of Convolutional Networks
ResNet, AlexNet, VGG, Inception: Understanding various architectures of Convolutional Networks by KO ...
- [转载]Linux中的网络接口及LO回环接口
转自:https://blog.csdn.net/weixin_39863747/article/details/80564358 Linux中的网络接口及LO回环接口 2018年06月04日 10: ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- C语言调用Python 混合编程
导语 Python有很多库,Qt用来编写界面,自然产生C++调用Python的需求.一路摸索,充满艰辛 添加头文件搜索路径,导入静态库 我的python头文件搜索路径:C:\Python27amd64 ...
- HDU 3635 Dragon Balls(带权并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意: 有n颗龙珠和n座城市,一开始第i颗龙珠就位于第i座城市,现在有2种操作,第一种操作是将x龙珠所在城 ...
- error LNK2019-无法解析的外部符号 _main-该符号在函数 ___tmainCRTStartup 中被引用
问题分析: 因为Win32 console Application的入口函数是Main(),而Win32 Application的入口函数才是WinMain() 解决方案: 右键项目,打开[属性]页, ...
- [从零开始搭网站三]CentOS配置JDK
点击下面连接查看从零开始搭网站全系列 从零开始搭网站 上一章我介绍了,如何不用每次都输密码连接服务器.那么这一章终于要开始服务器的开发环境配置了. 1:先输入以下代码来检验有没有已经安装的CDK: r ...