问题描述:

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. Java内存模型原理总结(转自51CTO)

    转载地址:http://developer.51cto.com/art/201811/587220.htm [51CTO.com原创稿件]这篇文章主要介绍模型产生的问题背景,解决的问题,处理思路,相关 ...

  2. JPA原理与实践、多数据源配置

    参考博客: https://segmentfault.com/a/1190000015047290?utm_source=Weibo&utm_medium=shareLink&utm_ ...

  3. maven web项目配置log4j,及log4j参数设置

    本文为博主原创,转载须注明转载地址: 1.在maven项目中引入相关的依赖: 需要依赖的jar为: <!-- 配置日志 --> <dependency> <groupId ...

  4. 【译】第14节---数据注解-MaxLength/MinLength

    原文:http://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribute-i ...

  5. Git 分支 - 远程分支

    Git 分支 - 远程分支 远程分支 远程分支(remote branch)是对远程仓库中的分支的索引.它们是一些无法移动的本地分支:只有在 Git 进行网络交互时才会更新.远程分支就像是书签,提醒着 ...

  6. Using keytool to import keystore

    open command line and locate to the location of  keytool.exe. import cert to keystore command: keyto ...

  7. JavaScript重点知识(一)

    一.总括 基础知识: 1.变量 2.原型和原型链 3.作用域和闭包 4.异步和单线程 JS的API: 1.BOM,DOM操作 2.事件绑定 3.Ajax 4.JSOP 5.存储 二.基础知识 2.1知 ...

  8. Thymeleaf的基本语法总结

    最近用Spring boot开发一些测试平台和工具,用到页面展示的部分, 选择的是thymeleaf模版引擎. 页面开发的7788快结束了,下面来总结下此过程中对thymeleaf的使用总结. 什么是 ...

  9. myeclipse2014配置多个同版本的Tomcat

    引言: 网上有很多myeclipse配置多个Tomcat的教程都可以参考,如[配置多个Tomcat1],[配置多个Tomcat2], 可以直接参考以上两个教程,我这里只对网上的教程中存在的一个点做说明 ...

  10. javaSE习题 第一章 JAVA语言概述

    转眼就开学了,正式在学校学习SE部分,由于暑假放视频过了一遍,略感觉轻松,今天开始,博客将会记录我的课本习题,主要以文字和代码的形式展现,一是把SE基础加强一下,二是课本中有很多知识是视频中没有的,做 ...