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?

题目大意:给定一个数组,长度为n,包含从0~n中的n个数,问哪个数不存在?

思路:加减就可以了。

    public int missingNumber(int[] nums) {
if(nums==null||nums.length==0){
return 0;
}
int res = 0;
for(int i=0;i<nums.length;i++){
res+=nums[i];
res-=i+1;
}
return -res;
}

Missing Number ——LeetCode的更多相关文章

  1. Missing Number @leetcode

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  2. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

  3. &lt;LeetCode OJ&gt; 268. Missing Number

    268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...

  4. [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 ...

  5. Leetcode-268 Missing Number

    #268.  Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...

  6. Missing number

    Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...

  7. hdu 5166 Missing number

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5166 Missing number Description There is a permutatio ...

  8. Missing Number, First Missing Positive

    268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find th ...

  9. HDU 5166 Missing number 简单数论

    Missing number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) [ ...

随机推荐

  1. Android中全局搜索(QuickSearchBox)详解

    http://blog.csdn.net/mayingcai1987/article/details/6268732 1. 标题: 应用程序如何全面支持搜索 2. 引言: 如果想让某个应用程序支持全局 ...

  2. Android开发手记(31) 使用MediaRecorder录音

    使用Android手机的时候,有时我们会用到录音功能,本文简单的介绍了如何使用MediaRecorder通过手机自带麦克风进行录音. 首先,既然是录音,我们需要录音和写外存的权限: <uses- ...

  3. java 进制.

    /* 整数的'3'种表现形式: 1,十进制. 2,八进制. 3,十六进制. */ public class IntegerDemo { public static void main(String[] ...

  4. SQL语句之三简单增删改查

    这是前面建的库和表 USE Test go INSERT dbo.MyTable --插入数据         ( NAME ,age) VALUES  ( '数据,20  -- NAME - var ...

  5. 【转】无废话WCF系列教程

    转自:http://www.cnblogs.com/iamlilinfeng/category/415833.html     看后感:这系列的作者李林峰写得真的不错,通过它的例子,让我对WCF有了一 ...

  6. Windows程序设计 贪吃蛇c

    看Windows程序有段时间了,终于动手写东西.贪吃蛇算是一个开始吧,下面的贪吃蛇很简单,也有很多地方需要修改,还有情况没有考虑QAQ 但这不是我的目的了... 思路很简单:建个链表储存蛇身节点即可. ...

  7. js编译和执行顺序

    JS是一段一段执行的(以<script>标签来分割),执行每一段之前,都有一个“预编译”,预编译干的活是:声明所有var变量(初始为undefined),解析定义式函数语句. 还有个关于 ...

  8. MySQL Procedure(MySQL存储过程)[转]

    ------Creating Stored Procedures in MySQL------ --Make sure you have version 5 of MySQL:   SELECT VE ...

  9. 去除VS2010中中文注释下的红色波浪线

    1,中文注释以分号结尾 2,Assist X 菜单栏->Assist X Option->Underline 选择“min”.

  10. 递归删除.DS_Store文件

    删除svn文件 sudo find . -name ".DS_Store" -exec rm -r {} \; sudo find . -name ".git" ...