题目描述:

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

Example 1:

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

Example 2:

Input: [9,6,4,2,3,5,7,0,1]
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

代码如下:

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

上述代码实测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. ios 7.1企业证书无线安装

    ios升级到7.1时,企业证书http服务器发布无线安装不能使用,需要使用https服务器. 测试了几种方式 1.测试时自签名https证书测试无效,提示不能连接到服务器 2.使用dropbox共享连 ...

  2. Django基础学习七之如何配置django+mysql

    很久没有更新博客了,也有段时间没有持续性的学习了,感觉堕落了,今天继续开始学习吧 今天主要来学习一下在django下配置mysql的数据库和使用admin用户管理数据库 1.在project中的set ...

  3. 数据结构notes

    1. 一份很好的数据结构教程,图文并茂,简明扼要,列出每种结构的定义和优缺点,非常适合初学者 via @ranyif https://www.interviewcake.com/data-struct ...

  4. VC++知识点整理

    1.内联函数        定义:定义在类体内的成员函数,即函数的函数体放在类体内        特点:在调用处用内联函数体的代码来替换,用于解决程序的运行效率问题.一定要在调用之前定义,并且内联函数 ...

  5. UITableView.separatorInset

    [UITableView.separatorInset] separatorInset指定每行row之间的分隔线的长度,iOS7.0后提供,官方文档如下: 示例截图如下,分隔线没有紧贴着左右边界:

  6. URL Scheme

    [URL Scheme] 可以通过info.plist注册url types来实现程序自定义的协议,以供外部程序调起. NSURL *myURL = [NSURL URLWithString:@&qu ...

  7. docker 构建 spring boot项目

    在docker 开始部署springBoot项目 1.在centos7 ~ 创建一个文件夹docker 里面放置 上面的Dockerfile 和 springBoot 打包的项目docker_spri ...

  8. 【译】微型ORM:PetaPoco【不完整的翻译】(转)

    出处:http://www.cnblogs.com/youring2/archive/2012/06/04/2532130.html PetaPoco是一款适用于.Net 和Mono的微小.快速.单文 ...

  9. Haodoop RPC解析

    1. 前言 1 2. Hadoop RPC 1 2.1. 总体结构 1 2.1.1. RPC Interface 1 2.1.2. RPC Server 1 2.1.3. RPC Client 1 2 ...

  10. linux每天一小步---find命令详解

    1 命令功能 find命令用于搜索指定目录下的文件,并配合参数做出相应的处理. 2 命令语法      find  搜索路径pathname 选项option [-exec -ok -print  执 ...