268 Missing Number 缺失的数字
给出一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。
案例 1
输入: [3,0,1]
输出: 2
案例 2
输入: [9,6,4,2,3,5,7,0,1]
输出: 8
注意事项:
您的算法应该以线性复杂度运行。你能否仅使用恒定的额外空间复杂度来实现它?
详见:https://leetcode.com/problems/missing-number/description/
Java实现:
方法一:
class Solution {
public int missingNumber(int[] nums) {
int res=nums.length;
int i=0;
for(int num:nums){
res^=num;
res^=i;
++i;
}
return res;
}
}
方法二:
class Solution {
public int missingNumber(int[] nums) {
int n=nums.length;
int sum=0;
for(int num:nums){
sum+=num;
}
return (int)(0.5*n*(n+1)-sum);
}
}
方法三:
用二分查找法算出中间元素的下标,然后用元素值和下标值之间做对比,如果元素值大于下标值,则说明缺失的数字在左边,此时将r赋为m,反之则将l赋为m+1。排序的时间复杂度都不止O(n),但是在面试的时候,有可能数组就是排好序的,那么此时用二分查找法肯定要优于上面两种方法。
class Solution {
public int missingNumber(int[] nums) {
Arrays.sort(nums);
int l=0;
int r=nums.length;
while(l<r){
int m=(l+r)>>1;
if(nums[m]>m){
r=m;
}else{
l=m+1;
}
}
return r;
}
}
参考:https://www.cnblogs.com/grandyang/p/4756677.html
268 Missing Number 缺失的数字的更多相关文章
- [LeetCode] 268. Missing Number 缺失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- LeetCode 268. Missing Number缺失数字 (C++/Java)
题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- [LeetCode] Missing Number 丢失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- 268. Missing Number@python
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- LeetCode 268. Missing Number (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 268. Missing Number序列中遗失的数字
[抄题]: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
随机推荐
- 【转】使用Python中HTTPParser模块进行简单的html解析
http://www.cnblogs.com/coser/archive/2012/01/09/2317076.html
- Python 出现 can't use a string pattern on a bytes-like object
Python 出现 can't use a string pattern on a bytes-like object 学习了:https://www.cnblogs.com/andrewleeeee ...
- CSS 全部的列表样式类型
<html> <head> <style type="text/css"> ul.none {list-style-type: none} ul ...
- 通过android XML 创建图形,降低对美工的依赖
在开发中总会须要自己定义各种View的样式,假设总是依赖美工作图弄出须要的UI样式图片(比方:一个button要选中和默认两张图片),不但时间上会浪费.往往也会有适配问题. 尽管能够通过.9图来解决一 ...
- BNU 25593 Prime Time 记忆化dp
题目链接:点击打开链接 题意: 一个游戏由3个人轮流玩 每局游戏由当中一名玩家选择一个数字作为開始 目的:获得最小的得分 对于当前玩家 O .面对 u 这个数字 则他的操作有: 1. 计分 u +1 ...
- luajit利用ffi结合C语言实现面向对象的封装库
luajit中.利用ffi能够嵌入C.眼下luajit的最新版是2.0.4,在这之前的版本号我还不清楚这个扩展库详细怎么样,只是在2.04中,真的非常爽. 既然是嵌入C代码.那么要说让lua支持 ...
- Android 5.1 Settings源代码简要分析
转载请注明出处,谢谢~http://blog.csdn.net/u011974987/article/details/51004854. 概述: 先声明:本人工作快两年了,仍是菜鸟级别的.羞愧啊!曾经 ...
- Android源代码文件夹结构说明
在学习Android的过程中,学习写应用还好.一開始不用管太多代码.直接调用函数就能够了,可是工作中却须要改动到framework之类的东东 所以感觉開始纠结了,又是初学,非常多不懂,所以就去找了关于 ...
- Android学习笔记-保存数据的实现方法1
Android开发中,有时候我们需要对信息进行保存,那么今天就来介绍一下,保存文件到内存,以及SD卡的一些操作,及方法,供参考. 第一种,保存数据到内存中: //java开发中的保存数据的方式 pub ...
- GraphDatabase_action
https://neo4j.com/docs/ #https://pypi.python.org/pypi/neo4j-driver/1.5.3from neo4j.v1 import GraphDa ...