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?

题目实际上很简单,就是要求求出一段o-n中缺了一个数的数列中找出缺失的那个,我一开始的想法是这样的:

 class Solution {
public:
int missingNumber(vector<int>& nums) {
int sz = nums.size();
for (int i = ; i < sz - ; ++i){
if (nums[i] != nums[i + ])
return nums[i + ];
}
return nums[sz - ] + ;
}
};

代码很简单,就是遍历比较而已。但是很明显的,不太符合题目对于常数项空间复杂度的要求,出去翻了翻答案,别人家的小孩是这样写的:

 class Solution {
public:
int missingNumber(vector<int>& nums) {
int sz = nums.size();
int total = (sz + )*sz / ;
for (int i = ; i < sz; ++i){
total -= nums[i];
}
return total;
}
};

这种被吊打的感觉,有点像高斯当时算随手算出5050吊打同伴小孩的情况。

java代码如下:

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

LeetCode OJ:Missing Number (丢失的数)的更多相关文章

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

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

  2. [LeetCode] 137. Single Number II 单独数 II

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

  3. [LeetCode] 247. Strobogrammatic Number II 对称数II

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  4. [LeetCode] 248. Strobogrammatic Number III 对称数III

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  5. [LeetCode] 260. Single Number III 单独数 III

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

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

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

  7. [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] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  9. [LeetCode] 264. Ugly Number II 丑陋数 II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

随机推荐

  1. BCrypt 加密实现

    Bcrypt百度百科: bcrypt,是一个跨平台的文件加密工具.由它加密的文件可在所有支持的操作系统和处理器上进行转移.它的口令必须是8至56个字符,并将在内部被转化为448位的密钥. 除了对您的数 ...

  2. 怎么找出解析失败的sql

    本文由我和公司同事问心共同测试分析完成. 很多时候我们会有这样一个误区,语法错误或者对象不存在应该在语法语义检查这个步骤就结束了,怎么还会存在共享池里面呢?带着这个几个问题我们做几个简单的测试. 我们 ...

  3. 5 TensorFlow入门笔记之RNN实现手写数字识别

    ------------------------------------ 写在开头:此文参照莫烦python教程(墙裂推荐!!!) ---------------------------------- ...

  4. jQuery文档节点处理,克隆,each循环,动画效果,插件

    文档节点处理 //创建一个标签对象 $("<p>") //内部插入 $("").append(content|fn) ----->$(&quo ...

  5. wechat多开

    右键wechat查看属性,找到目标(wechat的执行路径),复制 然后在桌面新建文档,输入下面命令,想多开几个就复制几行 start 复制的目标 另存为bat文件,所有文件类型 双击运行

  6. 有趣的Linux命令,让终端炫起来

    10条真心有趣的Linux命令 动画演示10个有趣但毫无用处的Linux命令 11个无用而有趣的Linux终端彩蛋 lolcat :一个在 Linux 终端中输出彩虹特效的命令行工具

  7. [笔记]Go语言的字符串拼装方式性能对比

    Go语言中字符串的拼装方法很多,那么问题来了,到底哪家性能好? 下面代码,分别比较了 fmt.Sprintf,string +,strings.Join,bytes.Buffer,方法是循环若干次比较 ...

  8. QQ空间动态内容,好友信息,点赞爬虫脚本

    一.安装基础的软件包: 1.准备好火狐浏览器,并下载geckodriver,将geckodriver加入到环境变量:下载geckodriver的地址:https://pan.baidu.com/s/1 ...

  9. python之路 面向对象基础 XML

    一.面向对象基础 1.类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义 ...

  10. linux命令详解之(at)

    在Linux下,有两个命令可以用来作为计划任务而执行,at:一次性定时任务计划执行crontab :每天定时任务计划执行 以下仅说一下一次性任务计划执行(at)要使用一次性任务计划,linux必须要有 ...