268. Missing Number

Total Accepted: 31740 Total
Submissions: 83547 Difficulty: Medium

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?

分析:DONE

我真蛋疼。题意理解错了,我还以为是干嘛呢!

后来才明确原来是随机从0到size()选取了n个数,当中仅仅有一个丢失了(显然的)。

别人的算法:数学推出,0到size()的总和减去当前数组和sum

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

这道问题被标注为位运算问题:參考讨论区的位运算解法:

这个异或运算曾经用到过,到这道题还是想不起这种方法,我真是日了狗了!

异或运算xor。

0 ^ a = a ^ 0 =a

a ^ b = b ^ a

a ^ a = 0

0到size()间的全部数一起与数组中的数进行异或运算,

由于同则0,0异或某个未出现的数将存活下来

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

注:本博文为EbowTang原创,兴许可能继续更新本文。

假设转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50457902

原作者博客:http://blog.csdn.net/ebowtang

&lt;LeetCode OJ&gt; 268. Missing Number的更多相关文章

  1. [LeetCode&Python] Problem 268. Missing Number

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

  2. LeetCode Array Easy 268. Missing Number

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

  3. 【LeetCode】268. Missing Number

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

  4. 268. Missing Number@python

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

  5. Java [Leetcode 268]Missing Number

    题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

  6. LeetCode 268. 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 ☆(丢失的数字)

    转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...

  8. [LeetCode] 268. Missing Number 缺失的数字

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

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

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

随机推荐

  1. 数塔问题mod 100(orz)

    看一下题目 和普通的数字三角形看似没啥区别(区别很大) 然后去想:DP方程 DP[i][j]=Max(DP[i-][j],DP[i-][j-])+a[i][j] ans=Max(DP[n][..n]) ...

  2. ACM_汉诺塔问题(递推dp)

    Problem Description: 最近小G迷上了汉诺塔,他发现n个盘子的汉诺塔问题的最少移动次数是2^n-1,即在移动过程中会产生2^n个系列.由于发生错移产生的系列就增加了,这种错误是放错了 ...

  3. Elasticsearch的索引模块(正排索引、倒排索引、索引分析模块Analyzer、索引和搜索、停用词、中文分词器)

    正向索引的结构如下: “文档1”的ID > 单词1:出现次数,出现位置列表:单词2:出现次数,出现位置列表:…………. “文档2”的ID > 此文档出现的关键词列表. 一般是通过key,去 ...

  4. 【转】Java 集合系列04之 fail-fast总结(通过ArrayList来说明fail-fast的原理、解决办法)

    概要 前面,我们已经学习了ArrayList.接下来,我们以ArrayList为例,对Iterator的fail-fast机制进行了解.内容包括::1 fail-fast简介2 fail-fast示例 ...

  5. Java&Xml教程(三)使用DOM方式修改XML文件内容

    DOM解析方式也可用于修改XML数据,我们可以使用它完成新增元素.删除元素.修改元素值.修改元素属性等操作. 我们的XML文件,内容如下: employee.xml <?xml version= ...

  6. iOS 从xib中加载自定义视图

    想当初在学校主攻的是.NET,来到公司后,立马变成java开发,之后又跳到iOS开发,IT人这样真的好么~~  天有不测风云,云还有变幻莫测哎,废话Over,let's go~ 新学iOS开发不久,一 ...

  7. Win32最简单的程序

    #include<tchar.h> #include<stdio.h> #include<windows.h> LRESULT CALLBACK WinSunPro ...

  8. web流行工具

    中小型公司: Node.js:现代工业化前端的基础: RequireJS:AMD规范, 即将过时的 JavaScript 模块化方案: Bower:前端模块源: npm:前端工具源,另一个潜在的前端模 ...

  9. CSS平滑过渡动画:transition

    <html> <head> <link href="http://cdn.bootcss.com/twitter-bootstrap/3.0.2/css/boo ...

  10. python文件头的含义

    一.指定解释器及其路径 在Linux\Mac上,可以用./文件路径直接运行.py文件 这时,需要在python文件开头指定解释器及其路径 #!/usr/bin/python 这样系统就直接按pytho ...