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 Studio设置行宽、格式化断行

    设置基于Android studio 1.2,其它版本可能位置不大一样,可以直接搜索 1.设置行宽 就是那条右标准线的位置:Setting-->Editor-->Code Style,右侧 ...

  2. mybatis分享

    Mybatis入门 一.Mybatis环境搭建及简单实例 pom.xml mybatis-config.xml <?xml version="1.0" encoding=&q ...

  3. 在用 JavaScript 工作时,我们经常和条件语句打交道,这里有5条让你写出更好/干净的条件语句的建议。

    1.多重判断时使用 Array.includes 2.更少的嵌套,尽早 return 3.使用默认参数和解构 4.倾向于遍历对象而不是 Switch 语句 5.对 所有/部分 判断使用 Array.e ...

  4. NUnit TestFixtureSetup 和 TestFixtureTearDown

    TestFixtureSetup 和 TestFixtureTearDown 在所有测试开始前(TestFixtureSetup)或结束后(TestFixtureTearDown)运行一 次.记住他只 ...

  5. Delphi 正则表达式之TPerlRegEx 类的属性与方法(1): 查找

    Delphi 正则表达式之TPerlRegEx 类的属性与方法(1): 查找 //查找是否存在 var   reg: TPerlRegEx; begin   reg := TPerlRegEx.Cre ...

  6. URAL 2081 Faulty dial

    题目: Faulty dial Pavel has not played ACM for ages, nor does he train teams, nor prepare problems. Th ...

  7. Generative model 和Discriminative model

    学习音乐自动标注过程中设计了有关分类型模型和生成型模型的东西,特地查了相关资料,在这里汇总. http://blog.sina.com.cn/s/blog_a18c98e50101058u.html ...

  8. maven项目,去除jar包中的不想要的依赖关系

    解释:就是说项目中要用到某一个a.jar包,通过maven引入了之后,也自动的导入了该jar包所依赖的包,这里就会存在一个问题,如果a.jar包依赖b.jar这个项目的1.0版本,可是我的项目中已经有 ...

  9. hadoop14---centos 安装activemq

    创建activemq目录 [root@node1 ~]# mkdir -p /usr/local/activemq 狐火下载activemq,从用户/download目录把文件cp到/usr/loca ...

  10. sql临时表的优点

    1: 临时表来组织数据,更高效的查询速度. 2:临时表的操作不会写入日志文件:好处:提高了 临时表操作的速度:坏处: 数据一旦丢失,无法恢复. 3: 临时表只允许当前会话框进行访问,因此不会担心死锁 ...