【数组】Missing Number
题目:
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
For example,
Given nums = [0, 1, 3] return 2.
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
思路:
先将数组排序,然后进行二分搜索。显然,中点的下标和中点的值相同时,说明从起始到中点没有错位,缺失数应该在数组后边。如果不相等,说明前面已经有错位,缺失数在左边。如果缺失数是最后一个的话,那整个数组都没有错位,则要返回最后一个加1。
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function(nums) {
if(nums.length==0){
return;
}
nums.sort(function(a,b){return a-b;});
var l=0,r=nums.length-1,middle=0;
while(l<r){
middle=l+Math.floor((r-l)/2);
if(middle!=nums[middle]){
r=middle-1;
}
if(middle==nums[middle]){
l=middle+1;
}
} return nums[l]==l?nums[l]+1:l
};
【数组】Missing Number的更多相关文章
- PAT 甲级 1144 The Missing Number (20 分)(简单,最后一个测试点没过由于开的数组没必要大于N)
1144 The Missing Number (20 分) Given N integers, you are supposed to find the smallest positive in ...
- Missing number
Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...
- Missing Number, First Missing Positive
268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find th ...
- [LintCode] Find the Missing Number 寻找丢失的数字
Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example G ...
- PAT 1144 The Missing Number[简单]
1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...
- 一道面试题Lintcode196-Find the Missing Number
http://www.lintcode.com/en/problem/find-the-missing-number/# Find the Missing Number Given an array ...
- 268. Missing Number@python
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number
数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...
- Leetcode-268 Missing Number
#268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...
随机推荐
- 功能强大的文件上传插件带上传进度-WebUploader
WebUploader是由Baidu WebFE(FEX)团队开发的一个以HTML5/FLASH构建的现代文件上传组件.在现代的浏览器里面能充分发挥HTML5的优势,同时又不摒弃主流IE浏览器,沿用老 ...
- 利用Project Tango进行室内三维建模 精度评定
coming soon 在Android开发基础上开发Tango应用 Android+Tango
- (KMP)剪花布条 -- hdu -- 2087
http://acm.hdu.edu.cn/showproblem.php?pid=2087 剪花布条 Time Limit: 1000/1000 MS (Java/Others) Memory ...
- hdu3333 Turing Tree 2016-09-18 20:53 42人阅读 评论(0) 收藏
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- MyEclipse2014中Java类右键Run as没有JUnit Test
Java初学,想试试连接本地数据库,按照百度经验中的方法,在最后执行测试的卡住了,为啥?因为MyEclipse中右键Run as没有JUnit Test选项! 6.1.测试数据库mysql是在项目中连 ...
- nodejs express hi-cms
今天看一下hi-cm是怎么写的,理解一下流程,看一些亮点 它使用express3.x写的 1.app.set, app.get app.set('port', 3000); app.get('port ...
- node.excel
今天突然间想起来用node如何操作excel,记得之前用Java的poi操作excel,感觉特别爽,计算机代替人的工作,非常有用,所以决定摸索一下. 在网上找了各种模块,有导出的,有导入的,有转为js ...
- Delphi Dll 动态调用例子(3)-仔细看一下
http://blog.163.com/bxf_0011/blog/static/35420330200952075114318/ Delphi 动态链接库的动态和静态调用 为了让人能快速的理解 静态 ...
- C#实现FTP文件的上传、下载功能、新建目录以及文件的删除
本来这篇博文应该在上周就完成的,可无奈,最近工作比较忙,没有时间写,所以推迟到了今天.可悲的是,今天也没有太多的时间,所以决定给大家贴出源码,不做详细的分析说明,如果有不懂的,可以给我留言,我们共同讨 ...
- 利用bulk添加百万条数据,进行测试
(1)连接数据库 public static void BulkToDB(DataTable dt) { //数据库连接 SqlConnection sqlCon = new SqlConnectio ...