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. 读取xml字符串

    string strXml = @"<MessageData><pm_id>10</pm_id><pm_title>这是公司或者产品的名称&l ...

  2. tomcat+nginx+redis实现均衡负载、session共享(二)

    今天我们接着说上次还没完成session共享的部分,还没看过上一篇的朋友可以先看下上次内容,http://www.cnblogs.com/zhrxidian/p/5432886.html. 1.red ...

  3. Hadoop的读写类调用关系_图示

  4. oracle官方文档- length篇

    一.首先介绍下单字节字符集和 多字节字符集 2.2字符编码方案 2.2.1 单字节编码     (1)单字节7位字符集,可以定义128个字符,最常用的字符集为 US7ASCII     (2)单字节8 ...

  5. 设置linux服务器定时与时间服务器同步

    在一些大公司经常出现这样一个情况:公司或一些机关单位的内部业务系统的应用服务器以及数据都是做的多机集群部署而且基本都是linux系统,而且都是内部网,不与外网通讯的.这样经常就会出现一个情况,我发送任 ...

  6. javascript的框架演化

    说起javascript不同的人或许有不同的看法,一些资深后台程序员在刚开始的时候根本没有把它当作是一门编程语言,但是随着后面js框架的出现,以及面向对象的程序设计,还有原型,闭包的不断使用,后台程序 ...

  7. 在用VC编译下debug和release的什么区别

    DEBUG和RELEASE 版本差异及调试相关问题:.         内存分配问题 1.          变量未初始化.下面的程序在debug中运行的很好. thing * search(thin ...

  8. 371. Sum of Two Integers -- Avota

    问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...

  9. 关于ASP.NET Session State Server

    最近公司开发的一个网站因为访问量增大,需要添加多台Web Server来进行负载均衡. 但是在做负载均衡前需要将一些原来固定存储在单台机器上的东西分离出来,使之能单独存在在一个独立的机器上,其中就有S ...

  10. 解决IE6不支持position:fixed;的问题

    在网页设计中,时常要用到把某个元素始终定位在屏幕上,即使滚动浏览器窗口也不会发生变化. 一般我们会使用position:fixed来进行绝对固定,但IE6并不支持position:fixed属性,所以 ...