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. Android 成功 使用GPS获取当前地理位置(解决getLastKnownLocation 返回 null)

    最近遇到一个比较棘手的问题:使用GPS定位无法获取当前的地理位置,即getLastKnownLocation方法始终返回null.   后来一篇博文 getLastKnownLocation()返回n ...

  2. go项目找不到包问题

    最近学习go语言,但是在主函数中引入其他包(自定义包)中方法的时候,编译代码,显示找不到包,如: $GOPATH>go build stacker.gostacker.go:18:2: cann ...

  3. golang的多协程实践

    go语言以优异的并发特性而闻名,刚好手上有个小项目比较适合. 项目背景: 公司播控平台的数据存储包括MySQL和ElasticSearch(ES)两个部分,编辑.运营的数据首先保存在MySQL中,为了 ...

  4. VGA显示

    VGA控制器的编写主要是了解VGA的显示标准和时序,如1024X768@60Hz,确定时钟频率(65MHz=1344X806X60),列像素时间等于时钟周期,扫描从左到右.从上到下(类似于电视扫描PA ...

  5. PHP 数组教程 定义数组

    数组array是一组有序的变量,其中每个变量被叫做一个元素. 一.定义数组  可以用 array() 语言结构来新建一个数组.它接受一定数量用逗号分隔的 key => value 参数对.  a ...

  6. django-ORM复习补充

    建表 class Author(models.Model): name = models.CharField(max_length=32) age = models.IntegerField() # ...

  7. windows10+mysql8.0.zip安装

    〇.准备: MySQL8.0 Windows zip包下载地址:https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.11-winx64.zip 环 ...

  8. $python打包工具pyinstaller的用法

    pyinstaller是一个很好用的python打包工具,在Windows环境下可以将python脚本打包成一个exe可执行文件,并且脚本中所依赖的各种第三方库在打包时候都会被统一处理到一起,这样打包 ...

  9. 关于git bash的问题,pull不下来(登录之后,git帮你记住了,想切换其他用户)

    参考博客: https://www.jianshu.com/p/8a7f257e07b8 从某个项目地址pull代码下来,老是报错 fatal: Authentication failed for ' ...

  10. MySQL-5.7 创建及查看触发器

    触发器的作用是当表上有对应SQL语句发生时,则触发执行. 1.语法 CREATE [DEFINER = { user | CURRENT_USER }] TRIGGER trigger_name tr ...