LeetCode-Find the Duplicate Number
来源:互联网 发布:淘宝详情图片怎么上传 编辑:程序博客网 时间:2024/06/05 05:26
查找数组中重复元素,仅有一个元素重复但可能重复多次。数组长度为n+1,1…n每个数字最多出现一次。
二分法,首先排序,因为整个数组的中位数是知道的,统计整个数组中小于n/2数字个数,若小于则重复数字在右边,反之在左边。
class Solution(object): def findDuplicate(self, nums): """ :type nums: List[int] :rtype: int """ length=len(nums)-1 l=0 r=length while l<r : find=(l+r)/2 lNum=0 for n in nums: if n <= find: lNum+=1 if lNum>find: r=find else: l=find+1 return l
阅读全文
0 0
- leetcode Find the Duplicate Number
- *LeetCode-Find the Duplicate Number
- [Leetcode] Find the Duplicate Number
- Leetcode -- Find the Duplicate Number
- LeetCode----Find the Duplicate Number
- LeetCode Find the Duplicate Number
- LeetCode -- Find the Duplicate Number
- [Leetcode]Find the Duplicate Number
- leetcode Find the Duplicate Number
- 【Leetcode】Find the Duplicate Number
- LeetCode Find the Duplicate Number
- Leetcode: Find the Duplicate Number
- leetcode: Find the Duplicate Number
- leetcode--Find the Duplicate Number
- 《leetCode》:Find the Duplicate Number
- LeetCode:Find the Duplicate Number
- [leetcode]-Find the Duplicate Number
- leetcode Find the Duplicate Number
- 二叉树中和为某一值的路径
- Hdu 6081 度度熊的王国战略 无向图全局最小割
- 静态链接器构造
- 消息中间件核心实体(0)
- 使用libvlc简单的播放一首本地音乐文件
- LeetCode-Find the Duplicate Number
- 刷紫书第三章习题(习题3-1到习题3-6)
- 数据挖掘——单层感知器的Matlab实现
- 《Thinking In Java》总结, 第二章:一切都是对象
- linux上安装git,并添加github SSHkeys
- net.sf.ezmorph.bean.MorphDynaBean cannot be cast to
- 每日一诗词 —— 题三义塔
- Java8系列之重新认识HashMap
- angularJs控制台报错之