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?


题目标签:Array

  题目给了我们一个nums array, array 里是从0 到n 的所有数字, 但是会缺失一个,让我们找出来,数字的排列不是有序的。

  既然我们知道n,而且数字是从0 到 n 的,可以利用等差数列求和公式 (首项+尾项) * 个数/2 求出总和, 再遍历一次nums 算出实际的sum, 然后差值就是缺失的那个数字了。

Java Solution:

Runtime beats 49.88%

完成日期:04/27/2017

关键词:Array

关键点:等差数列求和公式

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

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 268. Missing Number (缺失的数字)的更多相关文章

  1. [LeetCode] 268. Missing Number 缺失的数字

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

  2. [LeetCode] 268. Missing Number ☆(丢失的数字)

    转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...

  3. 268 Missing Number 缺失的数字

    给出一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数.案例 1输入: [3,0,1]输出: 2案例 2输入: [9,6,4,2,3,5,7, ...

  4. LeetCode 268. Missing Number缺失数字 (C++/Java)

    题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...

  5. Java [Leetcode 268]Missing Number

    题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

  6. LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告

    1.题目大意 Given an array nums, write a function to move all 0's to the end of it while maintaining the ...

  7. 33. leetcode 268. Missing Number

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

  8. leetcode 268 Missing Number(异或运算的应用)

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

  9. Leetcode 268 Missing Number 位运算

    题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...

随机推荐

  1. java:java构造器和java方法的区别

    构造函数(构造器)是一种特殊的函数.其主要功能是用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中.构造函数与类名相同,可重载多个不同的构造函数.在JA ...

  2. Linux入门_2-基础命令

    Linux入门-基础命令 目录 日期命令date 修改时区 日历命令cal 关机启动命令halt,reboot,poweroff whoami.who.who am i.w screen ...

  3. [js高手之路]Node.js+jade+mongodb+mongoose实现爬虫分离入库与生成静态文件

    接着这篇文章[js高手之路]Node.js+jade抓取博客所有文章生成静态html文件继续,在这篇文章中实现了采集与静态文件的生成,在实际的采集项目中, 应该是先入库再选择性的生成静态文件.那么我选 ...

  4. PHP 安装配置

    ./configure --prefix=/usr/local/php --with-libdir=/lib/x86_64-linux-gnu --with-config-file-path=/usr ...

  5. Opengl4.5 中文手册—G

    索引 A      B    C      D     E     F     G H      I     J      K     L     M     N O      P    Q      ...

  6. React Router 按需加载+服务器渲染的闪屏问题

    伴随着React协议的『妥协』(v16采用MIT),React为项目的主体,这个在短期内是不会改变的了,在平时使用过程中发现了如下这个问题: 在服务器渲染的时候,刷新页面会出现闪屏的现象(白屏一闪而过 ...

  7. Treblecross 博弈SG值

    Treblecross is a two player game where the goal is to get three X in a row on a one-dimensional boar ...

  8. 2013 ACM/ICPC Asia Regional Chengdu Online hdu4731 Minimum palindrome

    Minimum palindrome Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. hdu1356&hdu1944 博弈论的SG值(王道)

    S-NimProblem DescriptionArthur and his sister Caroll have been playing a game called Nim for some ti ...

  10. uva11806

    [题意] n行m列网格放k个石子.有多少种方法?要求第一行,第一列,最后一行,最后一列必须有石子. [题解] 利用容斥原理.可以转到求"第一行.第一列.最后一行.最后一列没有石子" ...