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?

这道题的话其实为了简单一点是需要用到一些数学知识的。我们已知的是这里面只有一个missing的数。

如果给你一个连续的整数列,计算总的sum的公式=[总个数*(总个数+1)]/2。

那么简化一点,先计算出应该有的sum然后减去现在的sum,得到的差就应该是那个我们miss的数了。

注意一点,我们的数列是从0开始,但是计算本应的sum的时候依然应该是[nums.length*(nums.length+1)]/2来计算,因为我们要加上一位作为被miss的数的位置。所以结果就是总个数还是nums.length不变。

代码如下。~

public class Solution {
public int missingNumber(int[] nums) {
int sum=0;
for(int i=0;i<nums.length;i++){
sum=sum+nums[i];
}
return (nums.length*(nums.length+1))/2-sum; }
}

[LeetCode] Missing Number (A New Questions Added Today)的更多相关文章

  1. [LeetCode] Missing Number 丢失的数字

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

  2. (leetcode)Missing Number

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

  3. [LeetCode] Ugly Number (A New Question Added Today)

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  4. LeetCode——Missing Number

    Description: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one t ...

  5. LeetCode Missing Number (简单题)

    题意: 给一个含有n个整数的数组,数组中的元素应该是0-n.现在缺了其中某1个,找出缺少的那个整数? 思路: 0-n的总和是可以直接计算的,而缺少的那个就是sum减去数组的和. int missing ...

  6. 【LeetCode】268. Missing Number

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

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

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

  8. [LeetCode] Single Number 单独的数字

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

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

随机推荐

  1. code manager tools svn服务安装配置

    svn server 安装配置: 下载地址:http://www.visualsvn.com/server/download/ 然后安装图一步一步前进: 1.点击download now: 2.点击N ...

  2. pycharm最新注册方法 pycharm最新激活方法 2016pycharm最新注册方法

    激活注册码:点击确认就可以了 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaW ...

  3. ConnectionReset

    ConnectionReset The connection was reset by the remote peer. http://stackoverflow.com/questions/1434 ...

  4. android 内存溢出问题分析

      最近的项目中,内存一直再增长,但是不知道是什么问题,导致内存溢出,在网上看到了这么一篇关于内存分析与管理的文章,解决了部分问题,感觉这篇文 章还不错,就转帖到我的blog上了,希望对大家有所帮助. ...

  5. 【MySQL】ERROR 1045 (28000): Access denied for user的解决方法

    去官网下载压缩版的MySQL Server,解压配置path环境变量后.然后克隆my-default.ini创建my.ini文件,在文件中[mysqld]下面配置basedir和datadir bas ...

  6. Linux进程控制(三)

    1. 进程间打开文件的继承 1.1. 用fork继承打开的文件 fork以后的子进程自动继承了父进程的打开的文件,继承以后,父进程关闭打开的文件不会对子进程造成影响. 示例: #include < ...

  7. 转:整理一下Entity Framework的查询

    Entity Framework是个好东西,虽然没有Hibernate功能强大,但使用更简便.今天整理一下常见SQL如何用EF来表达,Func形式和Linq形式都会列出来(本人更喜欢Func形式). ...

  8. 整理一些js中常见的问题

    原文链接 1.js获取select标签选中的值 原生js var obj = document.getElementByIdx_x(”testSelect”); //定位id var index =  ...

  9. 关于ios越狱开发的那些事

    也许吧,每每接触某些新东西的时候,都有点犯晕吧,这不是应该要的. 第一次接触ios越狱开发,也是这样吧.这篇主要是从无到有的说一下ios越狱的开发,网上很多的教程大部门都比较旧了吧,放在新设备上总是出 ...

  10. spring的声明式事务内部图