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. 在docker中制作自己的JDK+tomcat镜像

    准备工作:需要Linux kernel 3.8支持 查看linux内核的版本:root@ubuntu-dev:~# cat /proc/version查看linux版本:root@ubuntu-dev ...

  2. HDU1081:To The Max(最大子矩阵,线性DP)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1081 自己真够垃圾的,明明做过一维的这种题,但遇到二维的这种题目,竟然不会了,我也是服了(ps:猪啊). ...

  3. sersync的confxml.xml文件详解

    <?xml version="1.0" encoding="ISO-8859-1"?> <head version="2.5&quo ...

  4. image_Magic

    http://www.charry.org/docs/linux/ImageMagick/ImageMagick.html mogrify -sample 25% *.jpg  批量处理图片 conv ...

  5. python全栈开发从入门到放弃之网络基础

    一.操作系统基础 操作系统:(Operating System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才 ...

  6. 用Tchromium替换webbrowser

    用Tchromium替换webbrowser 用惯了EmbeddedWB,不想换,但是IE内核一直存在内存泄漏问题,没办法,只有寻找替代品了. 要把用习惯的EmbeddedWB换成完全不一样的TChr ...

  7. 在w3cschool学完html,css,javascript,jquery以后,还是不会做前端怎么办?

    w3cschool是一个非盈利性的在线技术学习网站,提供按W3C标准编写的基础教程.完整的看完w3cschool上面的手册,可以基本掌握编程语法.基础性的东西通常都会比较零散,因此,在学习一段时间后, ...

  8. tomcat web工程 jar包冲突解决方法

    目前在部署工程时,遇到了一个问题,报错信息如下: See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet ...

  9. 144. Binary Tree Preorder Traversal (二叉树前序遍历)

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  10. 使用 libevent 和 libev 提高网络应用性能(IBM)

    http://www.ibm.com/developerworks/cn/aix/library/au-libev/