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.

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

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

【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. Ubuntu上部署一个简单的Java项目

    一.安装tomcat7,mysql,Java JDK,直接apt安装 $ sudo aptitude install tomcat7 $ -jdk openjdk--jre $ sudo aptitu ...

  2. Spring IoC小结

    一. IoC是什么 Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象 ...

  3. 【区间dp】codevs1966 乘法游戏

    f(i,j)=min{f(i,k)+f(k,j)+a[i]*a[k]*a[j]}(1<=i<=j<=n,i<k<j) #include<cstdio> #in ...

  4. EXCEL某列长度超过255个字符导入SQL SERVER的处理方法

    问题描述: [Excel 源 [1]] 错误: 输出“Excel 源输出”(9) 上的 输出列“Description 3”(546) 出错.返回的列状态是:“文本被截断,或者一个或多个字符在目标代码 ...

  5. React Native填坑之旅--HTTP请求篇

    如果不能从头到尾的建立一个RN应用,那么RN将失色不少.本以为HTTP请求部分需要使用Native的实现,Android和iOS各回各家,各调各库了.Google了一下之后居然RN可以使用fetch库 ...

  6. [转]node.js学习笔记(二)

    二.express 1.安装 express4 npm --registry=http://registry.npmjs.org install -g express-generator (全局) 2 ...

  7. Address already in use: bind

    Eclipse中报了这个错误,下拉小窗口,可以看到正在运行的项目,选中项目,都关闭就Ok了 还有一种方法就是关闭javaw.exe进程

  8. ASP.NET 连接MySQL数据库 详细步骤

    ASP.NET默认的数据库是MS SQL Server,微软的数据库产品.事实上,如果不计成本因素的话,Windows Server + IIS + MS SQL Server + ASP.NET是网 ...

  9. C#分布式缓存二:Asp.Net中使用Couchbase

    前言 上一篇<C#分布式缓存一:Couchbase的安装与简单使用>主要讲解对Couchbase服务端的安装配置和客户端的引用调用,通过代码来完成最简单的实现调用.本次通过简单的配置,来完 ...

  10. 对bootstrap modal的简单扩展封装

    对bootstrap modal的简单扩展封装 参考自:http://www.muzilei.com/archives/677   注:原文不支持bootstrap新版本,并且居中等存在问题 此段时间 ...