给定一个包含n+1个整数的数组nums,这些整数都在[1, n]范围内。假设肯定存在一个重复的整数,求这个整数。
附加要求:
①不能修改数组(假设nums是只读的);
②空间复杂度要求为O(1);
③时间复杂度要求小于O(n2);
④数组中只有一个重复的数字,但是它可以重复多次。

条件①限制不能使用sorted()对原数组进行排序,条件②限制不能使用dict进行记录,条件③限制不能使用for循环嵌套。

二分法:
空间复杂度:O(1);时间复杂度:O(n logn)

数组首尾设置leftright,以数组最中间的数字下标(index)作为基准,称之为mid
然后遍历整个数组,若数组中小于mid的数字的个数小于或等于它,则多余数字的数值应该大于mid,即left=mid+1;若数组中小于mid的数字的个数大于它,则多余数字的数值应该小于mid,即right=mid-1
举例说明:mid=2。正常排序的话,count值应为2,数字为“1”“2”。若count>2,说明数字“1”“2”当中肯定有重复的,即所求的数字应该在前半部分,即right=mid-1

class Solution(object):def findDuplicate(self, nums):""":type nums: List[int]:rtype: int"""left = 0right = len(nums)-1while left<=right:mid = (left+right)/2count = 0for i in range(len(nums)):if nums[i] <= mid:count += 1if count <= mid:left = mid+1else:right = mid-1return left

38/100. Find the Duplicate Number相关推荐

  1. LeetCode 287. Find the Duplicate Number (时间复杂度O(n)) + 链表判断环

    LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...

  2. LeetCode 287. Find the Duplicate Number

    题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...

  3. LeetCode.287 Find the Duplicate Number

    题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...

  4. LintCode Find the Duplicate Number

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  5. LeetCode 287---Find the Duplicate Number

    问题链接:LeetCode 287-Find the Duplicate Number 题目大意 : 找出序列中唯一一个重复出现的数字,且只能使用o(1)的额外空间 实现代码如下: public cl ...

  6. 287. **Find the Duplicate Number

    287. **Find the Duplicate Number https://leetcode.com/problems/find-the-duplicate-number/description ...

  7. D19:Duplicate Number(重复数字,翻译+题解)

    原题:OpenJudge - 19:Duplicate Number 翻译: 描述:给定一个N个数的序列,求一个在序列中的至少出现2次的数A: 输入:第一行:一个不大于1000的正整数N : 第二行: ...

  8. leetcode 287. Find the Duplicate Number | 287. 寻找重复数(判断链表是否有环,并找到环的起点)

    题目 https://leetcode.com/problems/find-the-duplicate-number/ 题解 题目有限制 不能修改数组元素,必须 O(1) 空间复杂度,所以 不能排序, ...

  9. 633 - Find the Duplicate Number

    2017.9.11 感觉并没有啥意思啊这题,还是我误解了这题的真谛? public class Solution {/*** @param nums an array containing n + 1 ...

最新文章

  1. Java - 基本语法
  2. 深度学习时代的数据科学和自然语言处理
  3. 关于fragment backstate的运用
  4. Smark.Data 1.5更新详解
  5. apache ab压力测试报错
  6. 从rpm包中提取文件的命令
  7. 【java笔记】常用函数式接口(3):Predicate接口
  8. ESXi配置vCenter服务器
  9. 编译Windows版本ffmpeg:msys2方式失败
  10. JAVA分页查询实现
  11. Unity拓展——菜单栏拓展
  12. bmi计算器公式_BMI 计算器
  13. k8s标签选择器使用详解
  14. GXOI/GZOI2019题解
  15. 闲谈IPv6-Anycast以及在Linux/Win7系统上的Anycast配置
  16. 嵌入式编程中boot和app的s19简易合并方法(使用mfc编程)
  17. python实现三级菜单_Python3.5实现的三级菜单功能示例
  18. ctfshow web171-174
  19. STM32F4+CubeMX+Hal库下使能FPU
  20. 如何增强家里WiFi信号?

热门文章

  1. rust模组服如何切换标准服_送给玩模组服的萌新们
  2. mysql查询正在执行的存储过程,[转]ms sql server 存储过程,查看正在执行的sql语句...
  3. 51单片机驱动ds12887c语言,51单片机+DS12887+12864大数字时钟程序+电路
  4. hubliderx如选择相同单词_高考英语,十六种高效单词记忆法,建议人手一份!
  5. linux命令菜鸟ping,Linux ping命令
  6. 零基础学前端开发技术之第七章 浮动塌陷
  7. MyBatis增强工具pndao-帮你自动写SQL
  8. ssm整合之七 事务以及404页面处理
  9. python 向量_关于Python中的向量相加和numpy中的向量相加效率对比
  10. 动态规划(Dynamic Programming)例题步骤详解