问题描述:

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的更多相关文章

  1. HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  3. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  4. cf319.B. Modulo Sum(dp && 鸽巢原理 && 同余模)

    B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  5. poj 2356 Find a multiple(鸽巢原理)

    Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...

  6. [POJ3370]&[HDU1808]Halloween treats 题解(鸽巢原理)

    [POJ3370]&[HDU1808]Halloween treats Description -Every year there is the same problem at Hallowe ...

  7. [POJ2356]Find a multiple 题解(鸽巢原理)

    [POJ2356]Find a multiple Description -The input contains N natural (i.e. positive integer) numbers ( ...

  8. 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 ...

  9. [POJ2356] Find a multiple 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8776   Accepted: 3791   ...

随机推荐

  1. OpenWRT路由器使用ipv6拨号上网教程

    文章来源于群友,如有侵权,请联系我(aha971030@gmail.com)删除 原理介绍分析: 湖北E信地区可以使用ipv6拨号,好处是网络是上下对等不限速网络,也就是说,你的端口上限是多少,网上就 ...

  2. BZOJ4893: 项链分赃 && BZOJ4895: 项链分赃(增强版)

    Solution 神仙题.jpg 切一刀简单啊,维护一个前缀和. 切两刀简单啊,拿个队列维护中间那一段. 切三刀,这tm什么毒瘤题. 于是打开题解:"保证不会答案不会超过宝石种类" ...

  3. CF983B XOR-pyramid

    设\(xorx[l][r]\)表示题目中\(f(l,r)\)的值,则可以得出 \[ xorx[i][j]=xorx[i][j-1] \oplus xorx[i+1][j] \] 设\(maxx[l][ ...

  4. Twitter开发

    开发文档:https://developer.twitter.com/ the Twitter Developer Account Application 示例:https://wptweetboos ...

  5. Linux命令3——c

    cal:calender,显示月历 -j:用凯撒历(dates of julius caesar)的形式来显示月历,不分月份.1-365/366 -m:显示月历时,把星期一定为一周的开始.默认星期日为 ...

  6. 1.Jenkins 在windows下的安装与配置

    1. 安装Jenkins 1.war包安装:启动Jenkins命令,打开cmd至Jenkins安装目录下,运行命令 java -jar jenkins.war 如果改变默认端口,则指定端口例如端口号1 ...

  7. IDEA 的Class not found: "..."Empty test suite

    Junit测试的时候出现  IDEA 的Class not found: "..."Empty test suite问题. 尝试一下解决方法: 第一种方法: 1.modules&g ...

  8. 利用logstash从mysql同步数据到ElasticSearch

    前面一篇已经把logstash和logstash-input-jdbc安装好了. 下面就说下具体怎么配置. 1.先在安装目录bin下面(一般都是在bin下面)新建两个文件jdbc.conf和jdbc. ...

  9. Python—合并两个有序列表

    def hb(list1,list2): result = [] while list1 and list2: ] < list2[]: result.append(list1[]) del l ...

  10. ASP.net 网站开发知识点总结

    一.常用技术概括及介绍 1. SQL server:处理数据库的设计 2. asp.net  3. html            :前端网页 4. css :网页的布局设计 5. JavaScrip ...