Missing Number

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?

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

等差数列求和,易得缺失值。

  1. class Solution {
  2. public:
  3. int missingNumber(vector<int>& nums) {
  4. int n = nums.size();
  5. int expect = (n+) * n / ;
  6. for(int i = ; i < n; i ++)
  7. expect -= nums[i];
  8. return expect;
  9. }
  10. };

【LeetCode】268. Missing Number的更多相关文章

  1. 【LeetCode】268. Missing Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...

  2. 【easy】268. Missing Number

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

  3. 【leetcode】1228.Missing Number In Arithmetic Progression

    题目如下: 解题思路:题目很简单.先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失. 代码如下: class Solution(objec ...

  4. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

  5. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  6. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  7. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  8. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  9. 【LeetCode】163. Missing Range

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given a sorted integer array where the rang ...

随机推荐

  1. Win7 64位 VS2013环境编译boost1_58_0

    备忘,发现好多不常用的东西不记笔记再想用要重新花时间找,所以试着开始记笔记,写入博客吧. 首先去官网下最新的版本 http://www.boost.org/ 写本文时boost最新版本为1_58_0, ...

  2. Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  3. stopImmediatePropagation的应用

    在众多的方法里面,event.stopImmediatePropagation 算是比较少用的一个方法,拼写上感觉一半像 event.stopPropagation.对于stopPropagation ...

  4. 个性二维码开源专题<替换元素点>

    基础方法:ChangeFillShape //修改填充形状 ChangeFillShape(...) // 摘要: // 修改填充形状 // // 参数: // g: // 图形画板 // // Fo ...

  5. C#之玩转反射

    前言 之所以要写这篇关于C#反射的随笔,起因有两个:   第一个是自己开发的网站需要用到   其次就是没看到这方面比较好的文章. 所以下定决心自己写一篇,废话不多说开始进入正题. 前期准备 在VS20 ...

  6. 跟我一起学WCF(2)——利用.NET Remoting技术开发分布式应用

    一.引言 上一篇博文分享了消息队列(MSMQ)技术来实现分布式应用,在这篇博文继续分享下.NET平台下另一种分布式技术——.NET Remoting. 二..NET Remoting 介绍 2.1 . ...

  7. AngularJS Providers 详解

    供应者(Providers) Each web application you build is composed of objects that collaborate to get stuff d ...

  8. 旋转V字俄罗斯方块

    实现效果如图,也就是一个图像的旋转.注意,旋转后的文字是相对应的,而且文字还是立起的.第一次点击时显示,第二次点击时开始旋转.下面是我做这个效果的记录,方法这么差,我也就不说什么了. 先上HTML/C ...

  9. Atitit.并发编程原理与概论 attilax总结

    Atitit.并发编程原理与概论 attilax总结 1. 并发一般涉及如下几个方面:2 2. 线程安全性 ( 2.2 原子性 2.3 加锁机制2 2.1. 线程封闭3.3.1Ad-hoc线程封闭 3 ...

  10. JS open App(未安装就跳转下载页面)

    直接上代码var APPCommon = { downAppURl : "http://**/",//下载APP地址 downWeixin: "http://**&quo ...