38/100. Find the Duplicate Number
给定一个包含n+1
个整数的数组nums
,这些整数都在[1, n]
范围内。假设肯定存在一个重复的整数,求这个整数。
附加要求:
①不能修改数组(假设nums是只读的);
②空间复杂度要求为O(1);
③时间复杂度要求小于O(n2);
④数组中只有一个重复的数字,但是它可以重复多次。
条件①限制不能使用sorted()
对原数组进行排序,条件②限制不能使用dict
进行记录,条件③限制不能使用for循环
嵌套。
二分法:
空间复杂度:O(1);时间复杂度:O(n logn)
数组首尾设置left
和right
,以数组最中间的数字下标(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相关推荐
- LeetCode 287. Find the Duplicate Number (时间复杂度O(n)) + 链表判断环
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
- LeetCode 287. Find the Duplicate Number
题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...
- LeetCode.287 Find the Duplicate Number
题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...
- LintCode Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- LeetCode 287---Find the Duplicate Number
问题链接:LeetCode 287-Find the Duplicate Number 题目大意 : 找出序列中唯一一个重复出现的数字,且只能使用o(1)的额外空间 实现代码如下: public cl ...
- 287. **Find the Duplicate Number
287. **Find the Duplicate Number https://leetcode.com/problems/find-the-duplicate-number/description ...
- D19:Duplicate Number(重复数字,翻译+题解)
原题:OpenJudge - 19:Duplicate Number 翻译: 描述:给定一个N个数的序列,求一个在序列中的至少出现2次的数A: 输入:第一行:一个不大于1000的正整数N : 第二行: ...
- leetcode 287. Find the Duplicate Number | 287. 寻找重复数(判断链表是否有环,并找到环的起点)
题目 https://leetcode.com/problems/find-the-duplicate-number/ 题解 题目有限制 不能修改数组元素,必须 O(1) 空间复杂度,所以 不能排序, ...
- 633 - Find the Duplicate Number
2017.9.11 感觉并没有啥意思啊这题,还是我误解了这题的真谛? public class Solution {/*** @param nums an array containing n + 1 ...
最新文章
- Java - 基本语法
- 深度学习时代的数据科学和自然语言处理
- 关于fragment backstate的运用
- Smark.Data 1.5更新详解
- apache ab压力测试报错
- 从rpm包中提取文件的命令
- 【java笔记】常用函数式接口(3):Predicate接口
- ESXi配置vCenter服务器
- 编译Windows版本ffmpeg:msys2方式失败
- JAVA分页查询实现
- Unity拓展——菜单栏拓展
- bmi计算器公式_BMI 计算器
- k8s标签选择器使用详解
- GXOI/GZOI2019题解
- 闲谈IPv6-Anycast以及在Linux/Win7系统上的Anycast配置
- 嵌入式编程中boot和app的s19简易合并方法(使用mfc编程)
- python实现三级菜单_Python3.5实现的三级菜单功能示例
- ctfshow web171-174
- STM32F4+CubeMX+Hal库下使能FPU
- 如何增强家里WiFi信号?
热门文章
- rust模组服如何切换标准服_送给玩模组服的萌新们
- mysql查询正在执行的存储过程,[转]ms sql server 存储过程,查看正在执行的sql语句...
- 51单片机驱动ds12887c语言,51单片机+DS12887+12864大数字时钟程序+电路
- hubliderx如选择相同单词_高考英语,十六种高效单词记忆法,建议人手一份!
- linux命令菜鸟ping,Linux ping命令
- 零基础学前端开发技术之第七章 浮动塌陷
- MyBatis增强工具pndao-帮你自动写SQL
- ssm整合之七 事务以及404页面处理
- python 向量_关于Python中的向量相加和numpy中的向量相加效率对比
- 动态规划(Dynamic Programming)例题步骤详解