问题描述:

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. P4782 【模板】2-SAT 问题

    https://www.luogu.org/problemnew/show/P4782 链接 https://www.luogu.org/problemnew/show/P4782 思路 选a就必须选 ...

  2. oracle 之 如何链接别人电脑的oracle

    1.首先确保两台电脑是在同一个局域网内,可以通过cm命令窗口 ping 对方电脑的ID,若是没问题则表示可以连接 2.接下来通过配置来首先连接对方的电脑 其实在后面还有一个是否创建新的额服务名的操作, ...

  3. 深度学习课程笔记(十五)Recurrent Neural Network

    深度学习课程笔记(十五)Recurrent Neural Network 2018-08-07 18:55:12 This video tutorial can be found from: Yout ...

  4. 【论文笔记】Learning Convolutional Neural Networks for Graphs

    Learning Convolutional Neural Networks for Graphs 2018-01-17  21:41:57 [Introduction] 这篇 paper 是发表在 ...

  5. Derek解读Bytom源码-孤块管理

    作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...

  6. Global.asax.cs中相关方法

    protected void Session_Start(object sender, EventArgs e) { #if DEBUG //debug 登陆默认设置 #endif } protect ...

  7. Python GIL(Global Interpreter Lock)

    一.介绍 In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threa ...

  8. python类的成员

    一.实例变量:简单的来说就是给对象赋值 class Person: def __init__(self, name, card_no, height, weight, address, laopo): ...

  9. git 命令 clone分支的代码

    一个项目通常含有很多分支, master分支一般是经过测试,验证没有问题后,代码才会提交到master分支 develop分支,是测试经常拉下来进行测试的分支 直接复制develop分支的git 命令 ...

  10. java io 好文传送

    转自:白大虾 地址:https://www.cnblogs.com/baixl/p/4170599.html 主要内容 java.io.File类的使用 IO原理及流的分类 文件流 FileInput ...