题目描述:

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

Example 1:

  1. Input: [3,0,1]
  2. Output: 2

Example 2:

  1. Input: [9,6,4,2,3,5,7,0,1]
  2. Output: 8

Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

要完成的函数:

int missingNumber(vector<int>& nums)

说明:

1、给定一个vector,长度假设为K,那么从vector中找到一个在[0,K]之间没有出现过的数字,返回这个数字。

2、这道题十分容易,知道异或的同学,这道题都能半分钟内AC。

异或的原理简单介绍下,比如vector是[0,1,3],那么我们把[0,3]之间的数都跟vector中的数[0,1,3]做异或,结果是0^1^2^3^0^1^3=2,也就能出现那个只出现一次的值。

为什么能这样,因为异或支持交换律和结合律,上面的式子,我们其实能重新写成(0^0)^(1^1)^2^(3^3)。

同时,异或的规则是两个相同的数异或结果为0,两个不同的数异或结果是1(二进制的表示方法)

所以(0^0)结果肯定是0,(1^1)也就是0001^0001=0000=0,(3^3)=0011^0011=0000=0,所以0^2=0000^0010=0010=2。所以最终结果为2。

我们可以用异或来找到那个只出现一次的数字。

对于异或更多细节感兴趣的同学,可以看一下笔者之前写的另一篇题解leetcode-136-Single Number

代码如下:

  1. int missingNumber(vector<int>& nums)
  2. {
  3. int s1=nums.size(),res=0;
  4. for(int i=0;i<s1;i++)//跟nums中的元素和[0,k-1]中的数字不断异或
  5. res=res^nums[i]^i;
  6. return res^s1;
  7. }

上述代码实测24ms,beats 91.26% of cpp submissions。

leetcode-268-Missing Number(异或)的更多相关文章

  1. leetcode 268 Missing Number(异或运算的应用)

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

  2. Java [Leetcode 268]Missing Number

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

  3. [LeetCode] 268. Missing Number ☆(丢失的数字)

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

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

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

  5. 33. leetcode 268. Missing Number

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

  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缺失数字 (C++/Java)

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

  8. LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告

    1.题目大意 Given an array nums, write a function to move all 0's to the end of it while maintaining the ...

  9. Leetcode 268 Missing Number 位运算

    题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...

  10. &lt;LeetCode OJ&gt; 268. Missing Number

    268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...

随机推荐

  1. asp.net页如何获取母版页控件

    获取母版页的相关内容有两种方法 1 通过findcontrol找控件ID需要在此事件中~因为Page_load中时是先内容页加载然后才是母版页加载 protected void Page_LoadCo ...

  2. sqlserver备份与还原

    备份:数据库右键 默认: 还原成功: 还原: 1,新建同名数据库,右键 下步一定要,不然会报“备份集中的数据库备份与现有的数据库不同” 还原成功后

  3. MongoServerSettings Members

    The MongoServerSettings type exposes the following members. Constructors   Name Description MongoSer ...

  4. 5-青蛙的约会(ex_gcd)

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:122411   Accepted: 25980 Descripti ...

  5. innodb count优化测试

    对于索引优化真的是门课题,先来研究下最平常的问题,innodb引擎下 怎么让count(*)快一点. 首先需要清楚 innodb 默认是对主键建立聚簇索引,如果没有主键,那就是对具有唯一且非空值的索引 ...

  6. UI设计可供性解析:巧用隐藏的设计力提升用户体验

    以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 在实际的Web或App界面设计中,设计师们在学习和实践各种专业知识和技能之外,也会不可避免的遇到到各 ...

  7. B-spline Curves 学习之B样条曲线的系数计算与B样条曲线特例(6)

    B-spline Curves: Computing the Coefficients 本博客转自前人的博客的翻译版本,前几章节是原来博主的翻译内容,但是后续章节博主不在提供翻译,后续章节我在完成相关 ...

  8. Oracle E-Business Suite R12.1.x Installation And Upgrade Guide Step by Step

    1.        Install Oracle E- Business Suite R12.1.1       2.        Upgrade E- Business Suite From 12 ...

  9. php数组转成php编程代码

    将php数组转成可以在php上面运行的编程代码,支持一维及多维数组 <?php //一维数组 $test1 = array(1,2,3); //二维数组 $test2[0] = array( ' ...

  10. freemarker的include标签

    和jsp的include标签的作用类似,里面也是分相对路径和绝对路径. freemarker的根路径比较奇特的,结构图如下: ----webroot-------page------------ftl ...