博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
287. Find the Duplicate Number - Medium
阅读量:6939 次
发布时间:2019-06-27

本文共 1740 字,大约阅读时间需要 5 分钟。

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.

Example 1:

Input: [1,3,4,2,2]Output: 2

Example 2:

Input: [3,1,3,4,2]Output: 3

Note:

  1. You must not modify the array (assume the array is read only).
  2. You must use only constant, O(1) extra space.
  3. Your runtime complexity should be less than O(n2).
  4. There is only one duplicate number in the array, but it could be repeated more than once.

M1: 在0~n-1范围内,遍历数组。如果nums[abs(nums[i])] >= 0,把它变成小于0;如果< 0,则该数为重复数

时间O(N),空间O(1)

class Solution {    public int findDuplicate(int[] nums) {        int i;        for(i = 0; i < nums.length; i++) {            if(nums[Math.abs(nums[i])] >= 0)                nums[Math.abs(nums[i])] = - nums[Math.abs(nums[i])];            else                break;        }        return Math.abs(nums[i]);    }}

 

M2: 用binary search,对于由1~n构成的数组,数组长度(nums.length = n + 1)总大于数组里的任意数字,mid = length/2。如果每个数只出现一次,那么必然在mid两边有相同数量的数。如果某一边多了,说明重复的数在这边,可以缩小范围。

从index为1的元素开始统计比较方便。从1~lengh-1,mid是中位数,遍历数组统计<= mid的元素个数cnt,如果没有重复数,<= mid的个数应该大于mid(=mid+1)。在已知存在重复数的情况下,如果<= mid的元素个数<= mid,说明重复数在右侧,l = mid+1;如果>mid,说明在左侧,r = mid。不断重复直到 l >= r,最后的 l 就是答案。

时间O(NlogN),空间O(1)

class Solution {    public int findDuplicate(int[] nums) {        int l = 1, r = nums.length - 1;        while(l < r) {            int mid = l + (r - l) / 2, cnt = 0;            for(int i : nums) {                if(i <= mid)                    cnt++;            }            if(cnt <= mid)                l = mid + 1;            else                r = mid;        }        return l;    }}

 

转载于:https://www.cnblogs.com/fatttcat/p/9986854.html

你可能感兴趣的文章
MySQL 通配符学习小结
查看>>
Java框架----SSH整合回顾
查看>>
我的学习笔记_Windows_HOOK编程 2009-12-03 11:19
查看>>
POJ 1845 (约数和+二分等比数列求和)
查看>>
Android tab_Host页面跳转,传值,刷新等问题汇总
查看>>
[kmp+dp] hdu 4628 Pieces
查看>>
反射简介—类型反射和晚期绑定
查看>>
Lintcode: Binary Tree Serialization (Serialization and Deserialization Of Binary Tree)
查看>>
[设计模式] 9 装饰者模式 Decorator
查看>>
beetle.express针对websocket的高性能处理
查看>>
bat批处理设置Java JDK系统环境变量文件
查看>>
Javascript的setTimeOut()和setInterval()的定时器用法
查看>>
HDU 4819 Mosaic D区段树
查看>>
商务部
查看>>
ASP.Net MVC开发基础学习笔记(5):区域、模板页与WebAPI初步
查看>>
python静态方法和类方法
查看>>
iOS实现地图半翻页效果--老代码备用参考
查看>>
走过电竞之路的程序员
查看>>
JQ 获取地址栏参数
查看>>
关于AFNetworking访问网络超时的设置
查看>>