给定一个数组nums,其中包含0--n中的n个数,找到数组中没有出现的那个数。

解法一:cyclic swapping algorithm

class Solution
{
public:
int missingNumber(vector<int>& nums)
{
nums.push_back(-);
int len=nums.size();
for(int i=;i<len;i++)
{
while(nums[i]>= && nums[i]!=i)
swap(nums[i], nums[nums[i]]);
}
for(int i=;i<=len;i++)
if(nums[i]==-)
return i;
return -;
}
};

解法二:用(1+n)*n/2减掉数组中所有数,就是没有出现的那个数。

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

解法三:使用异或运算符,a^b^b=a。

class Solution
{
public:
int missingNumber(vector<int>& nums)
{
int result = nums.size();
for(int i=;i<nums.size();i++)
result = result^i^nums[i];
return result;
}
};

leetcode_268.missing number的更多相关文章

  1. Leetcode-268 Missing Number

    #268.  Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...

  2. Missing number

    Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...

  3. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

  4. hdu 5166 Missing number

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5166 Missing number Description There is a permutatio ...

  5. Missing Number, First Missing Positive

    268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find th ...

  6. HDU 5166 Missing number 简单数论

    Missing number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) [ ...

  7. Missing number in array

    Given an array of size n-1 and given that there are numbers from 1 to n with one missing, the missin ...

  8. PAT 1144 The Missing Number

    1144 The Missing Number (20 分)   Given N integers, you are supposed to find the smallest positive in ...

  9. [LintCode] Find the Missing Number 寻找丢失的数字

    Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example G ...

随机推荐

  1. 「SHOI2007」「Codevs2341」 善意的投票(最小割

    2341 善意的投票 2007年省队选拔赛上海市队选拔赛 时间限制: 5 s 空间限制: 128000 KB 题目等级 : 大师 Master   题目描述 Description 幼儿园里有n个小朋 ...

  2. NIO知识摘录

    在 JDK 1. 4 中 新 加入 了 NIO( New Input/ Output) 类, 引入了一种基于通道和缓冲区的 I/O 方式,它可以使用 Native 函数库直接分配堆外内存,然后通过一个 ...

  3. htm5 vido.js 播放器

    js统制html5 [video]标签中视频的播放和停止   需求:页面中有2个普通按钮a,b.还有一个video标签,能成功播放出视频..我想要的效果是,点击a按钮,视频开始播放,点击b按钮,视频播 ...

  4. UVa 1643 Angle and Squares (计算几何)

    题意:有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得 ...

  5. 左耳朵耗子:我对 GitLab 误删除数据库事件的几点思考

    参考链接:https://www.infoq.cn/article/some-thoughts-on-gitlab-accidentally-deleting-database 太平洋时间 2017 ...

  6. 7天学完Java基础之3/7

    API概述 什么叫做API? API(Application Programming lnterface),应用程序编程接口. 所谓API就是值好多的类,好多的方法,JDK给我们提供了很多现成的类,我 ...

  7. NOIp 2015 Day1T3斗地主【搜索】

    题目传送门 昨天真题测试赛题目== 没想到一道纯到都不用剪枝的搜索会是noipT3难度. 不过因为我搜索弱啊所以打不出来== LA:这不就是一道简单模拟题么 码完此题能增加对搜索的理解== (闲话结束 ...

  8. Redis生成主键ID

    使用Redis來生成主鍵ID策略,這裡主要使用 RedisAtomicLong 類來實現主鍵生成策略.具體代碼如下: /** * @Description: 获取自增长值 * @param key k ...

  9. 2018 年度码云热门项目排行榜 TOP 10

    2016 年度码云热门项目排行榜 TOP 10 是通过开源项目2016年在码云上的 Watch.Star.Fork 数量来评定的榜单.码云平台发展至今,涌现了越来越多优秀的开源项目,越来越多的开源作者 ...

  10. C++面向对象程序设计举例

    [例8.1]最简单的例子. #include <iostream> using namespace std; class Time //定义Time类 { public : //数据成员为 ...