【数组】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 ...
随机推荐
- ansible-playbook api 2.0 直接运行
官方文档见 http://docs.ansible.com/ansible/dev_guide/developing_api.html 拿官方的例子修改如下 import json from col ...
- ansible-playbook 主机变量2
ansible-playbook 配置 hosts 后可以指定变量,通过-k 可以交互输入密码,也可以将密码写在 hosts 文件中. 入口 yaml 文件中通过 {{ ** }} 获取变量,命令行通 ...
- JSTL自定义函数完成ACL即时认证
即时认证是指,用户进行查询或更新操作时,判断该用户进行是否对该操作有权限. 这里以判断用户是否有删除权限为例.如果用户有删除权限,即显示该按钮:如果没有删除权限,则不显示该按钮. 1.Manager层 ...
- Why I Want A Wife
I want a wife who will take care of my physical needs. I want a wife who will keep my house clean. A ...
- 一个CSV文件解析类
import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.ut ...
- SpringBoot tomcat
该文的前提是已经可以在控制台运行成功,有些时候想放在tomcat里运行 大致分为如下几步 1.配置文件更改 <dependency> <groupId>org.springfr ...
- 利用backtrace和backtrace_symbols函数打印调用栈信息
在头文件"execinfo.h"中声明了三个函数用于获取当前线程的函数调用堆栈. #include <execinfo.h> int backtrace(void * ...
- Django:上传文件或者图片时request.FILES的值为空
在form表单中加上属性 enctype="multipart/form-data"
- ASP.NET控制HTTP缓存
请求 响应 If-Modified-Since Last-Modified If-None-Match ETag 至于Expires和Cache-Control 附上几张高手的图 Etag 在使用 ...
- Godot开发环境与学习资源
记得第一次听到Godot的名字时还在深圳工作,刚刚接触Unity快一年,只是匆匆在Github上star了,就没有继续了.后面时不时翻开它的Git主页,然而一直没有下载过,每每想看看开源引擎都去看Or ...