给定一个数组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. 【前端】Element-UI 省市县级联选择器 JSON数据

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/element_cascader.html 不想自己处理的就直接下载吧 http://shamoyuu.bj.bce ...

  2. 编程 MD(d)、MT(d)编译选项的区别

    转:http://blog.csdn.net/nodeathphoenix/article/details/7550546 1.各个选项代表的含义 编译选项 包含 静态链接的lib 说明 /MD _M ...

  3. LXD安装

    #安装 #初始化(一路next) sudo lxd init #启动容器 lxc launch ubuntu:16.04 first #进到容器内 lxc exec first -- /bin/bas ...

  4. CodeForces 718A Efim and Strange Grade (贪心)

    题意:给定一个浮点数,让你在时间 t 内,变成一个最大的数,操作只有把某个小数位进行四舍五入,每秒可进行一次. 析:贪心策略就是从小数点开始找第一个大于等于5的,然后进行四舍五入,完成后再看看是不是还 ...

  5. Tomcat黑窗口改变Title

    start cmd /K " && call startup.bat && pause && exit " 设置Title之后,再手 ...

  6. lightoj1169【DP】

    题意(来自大哥): 有两栋楼,左边一栋,右边一栋,层数从1-n,地面的标号为0,每一层有一个水果.有一只猴子在地面上,他现在要上到n层去,在第i层会吃掉水果花费一定时间. 猴子有两种方式从第i层到i+ ...

  7. hdoj5387【模拟】

    题意: 略: 思路: 把所有的角度按照分母的形式写,中间不要约,不要除...(然后我就wa了),本来是想保证结果的正确性,最后会造成约好以后分子很大..>360°: /* 这个案例不错,妈的,随 ...

  8. CodeForces 622C

    题意: 给你一个数组,m个询问,l,r,x;让你输出在区间[ l , r ]上哪个位置不等于x. 思路: 额..我这个思路还是剽来的...不过真心赞啊. 开个p数组,直接记录数组每个元素的位置,并且实 ...

  9. POJ2446【建图建图】

    题意: 给你一个n*n的矩阵,然后再给你几个坑,然后问你能否被1*2的长方形给覆盖: -弱知道了是二分匹配的做法,但是弱还是不会转化,又是在建图上GG了 分析: 从国际象棋的那个黑白色理解,这是一张二 ...

  10. 无向图的边双连通分量(EBC)

    嗯,首先边双连通分量(双连通分量之一)是:在一个无向图中,去掉任意的一条边都不会改变此图的连通性,即不存在桥(连通两个边双连通分量的边),称作边双连通分量.一个无向图的每一个极大边双连通子图称作此无向 ...