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. Backbone Backbone-localStorage demo

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. BZOJ 1018 堵塞的交通traffic(线段树)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1018 题意:一个2*n的格子,相邻格子之间有一条道路.初始时道路是不通的. 三种操作:( ...

  3. ajax请求(二),后台返回的JSon字符串的转换

    ajax请求,json的转换 $.ajax({ url : "../folder/isExistAddFolder.do?t="+new Date(), type : 'POST' ...

  4. Toad

    1. Toad 规矩: toad 不会违反, 限制, 扩大 你当前用户的权限, toad 不会影响你定义的关于instance的内容. 2. toad 可以执行大部分在 sql*plus 中执行的命令 ...

  5. Python3 学习第九弹: 模块学习二之文件管理模块

    os模块 提供访问操作系统的接口 1> name 获得当前操作系统 其中 'nt' 是 windows 'posix' 是 linux 2> environ 获得当前系统的环境变量的字典, ...

  6. [转载]hao123军事频道首页JQ焦点图

    适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. *本作品由[站长素材]收集整理,转载请注明出处! 下载地址

  7. UVa 1225 Digit Counting

    题意:给出n,将前n个整数顺次写在一起,统计各个数字出现的次数. 用的最笨的办法--直接统计-- 后来发现网上的题解有先打表来做的 #include<iostream> #include& ...

  8. BZOJ 1202 狡猾的商人

    前缀和+带权并查集. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...

  9. HDU 5371 Hotaru's problem (Manacher,回文串)

    题意:给一个序列,找出1个连续子序列,将其平分成前,中,后等长的3段子序列,要求[前]和[中]是回文,[中]和[后]是回文.求3段最长为多少?由于平分的关系,所以答案应该是3的倍数. 思路:先Mana ...

  10. 最大熵模型 Maximum Entropy Model

    熵的概念在统计学习与机器学习中真是很重要,熵的介绍在这里:信息熵 Information Theory .今天的主题是最大熵模型(Maximum Entropy Model,以下简称MaxEnt),M ...