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. Asp.net主题(theme)和皮肤(skin)的使用

    asp.net 的服务器端控件提供了多种样式的设计,如果对每个控件都单独设置,是比较繁琐的事情,所以微软也提供了针对这些服务器端控件的样式管理,其实也可以通过 css来控制部分服务器端控件的样式,比如 ...

  2. SQLite 入门教程(二)创建、修改、删除表

    一.数据库定义语言 DDL 在关系型数据库中,数据库中的表 Table.视图 View.索引 Index.关系 Relationship 和触发器 Trigger 等等,构成了数据库的架构 Schem ...

  3. Linux命令:cat命令详解

    概述:查看文件内容,连接文件,重定向输出到文件 1.查看整个文件 2.cat > filename 创建文件 3.合并输出到文件 1.查看文件(单个或者多个) cat demo.txt 2.创建 ...

  4. Android界面布局学习总结

    参考文章: http://blog.csdn.net/shakespeare001/article/details/7843460 http://www.cnblogs.com/w-y-f/p/412 ...

  5. GridBagLayout的帮助类

    自备详细注释 /* * To change this license header, choose License Headers in Project Properties. * To change ...

  6. ASP.Net数据导出Excel的几种方法

    方法一 通过GridView(简评:方法比较简单,但是只适合生成格式简单的Excel,且无法保留VBA代码),页面无刷新 aspx.cs部分 代码如下: using System; using Sys ...

  7. H5的本地存储

    localStorage(本地存储),可以长期存储数据,没有时间限制,一天,一年,两年甚至更长,数据都可以使用.sessionStorage(会话存储),只有在浏览器被关闭之前使用,创建另一个页面时同 ...

  8. JavaScript中document.cookie

    “某些 Web 站点在您的硬盘上用很小的文本文件存储了一些信息,这些文件就称为 Cookie.”—— MSIE 帮助.一般来说,Cookies 是 CGI 或类似,比 HTML 高级的文件.程序等创建 ...

  9. vim代码折叠功能

    问题:怎样在vim中实现代码折叠功能? 解决方法:直接使用vim自带的快捷键和命令,便可以实现功能强大的折叠 小试折叠: 1  :set fdm=marker  在vim中执行该命令 2  5G  将 ...

  10. 修改app名字

    一张图说明问题 如果没有成功clean一下,或者卸载掉原有的重新生成一下 如果要修改路径名和工程名有个复杂的方法 http://blog.sina.com.cn/s/blog_a42013280101 ...