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. html5——动画案例(时钟)

    1.秒钟转360度需要60s分60步 2.分针转360度需要3600s分60步 3.秒钟转360度需要43200s分60步 <!DOCTYPE html> <html lang=&q ...

  2. JS——全选与全不选

    1.每个子input标签都需要进行判断 2.使用开闭原则,一旦满足条件就改变默认值 3.在给主input标签注册事件时,要求主input标签的checked值赋值给子标签 <!DOCTYPE h ...

  3. Linux基础之网络协议

    互联网通信原理 从物理层面来说,每台计算机在一开始都是彼此孤立的,为了实现信息的交流与共享,计算机之间必须要建立通信网络.例如人与人之间的交流,他们必须要共用一套语言系统,才能交流成功.计算机之间也是 ...

  4. CSS3利用box-shadow实现相框效果

    CSS3利用box-shadow实现相框效果 <style> html { overflow: hidden; background-color: #653845; background- ...

  5. Codeforces_731F_(前缀和)

    F. Video Cards time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. 大白_uva10795_新汉诺塔

    题意:给出所有盘子的初态和终态,问最少多少步能从初态走到终态,其余规则和老汉诺塔一样. 思路: 若要把当前最大的盘子m从1移动到3,那么首先必须把剩下的所有盘子1~m-1放到2上,然后把m放到3上. ...

  7. java_randomAccessFile类的使用

    RandomAccessFile java提供的对文件内容的访问,既可以读文件,也可以写文件.RandomAccessFile支持随机访问文件,可以访问文件的任意位置 (1)java文件模型 在硬盘上 ...

  8. eclipse的任务列表

    如上图所示,备注加  TODO  ,可以在tasks列表中显示,提示你还有哪些工作需要完善 昨天遇到一个问题,加了  TODO  任务列表里却不显示,后来发现是因为任务列表只显示了前100条,而我的项 ...

  9. java静态变量、实例变量和局部变

    实例变量又称成员变量: 1⃣️成员变量定义在类中,在整个类中都可以被访问 2⃣️成员变量随着对象的建立而建立,随对象的消失而消失,存在于对象所在的对内存中 3⃣️成员变量有默认初始值 局部变量: 1⃣ ...

  10. C#工具帮助类

    md5帮助类 .引入库 using System.Security.Cryptography;//引用Md5转换功能 .计算字符串的Md5值 public static string GetMD5Wi ...