题目描述:

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. DNS开源服务器BIND最小配置详解

    一,简介 相对于存储和大数据领域,CDN是一个相对小的领域,但行行出状元,BIND就是CDN领域的蝉联N届的状元郎.BIND是一款非常常用的DNS开源服务器,全球有90%的DNS用BIND实现.值得一 ...

  2. 8-cin cout PK scanf printf(速度快慢问题对比)

    我们在c++ 中使用cin cout很方便但速度很慢,导致有些题目用cin就超时而用scanf则就ac了,那到底改用谁? cin慢是有原因的,其实默认的时候,cin与stdin总是保持同步的,也就是说 ...

  3. SpringMVC学习总结(一)--Hello World入门

    一.什么是Spring MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 ...

  4. Vue.js基础知识

    <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml&q ...

  5. code1105 过河

    dp方程很简单: f[i] = min{ f[i-j] } + stone[i] 但是数据10^9太大了,超时超空间,这样只能过30% 来自:http://blog.csdn.net/w1996070 ...

  6. wcf已知类型 known type

    .服务契约的定义 /* Copyright (c) 2014 HaiHui Software Co., Ltd. All rights reserved * * Create by huanglc@h ...

  7. TOGAF架构培训材料学习总结

        作于一个架构师尤其是企业架构师来说,丰富的理论知识可以帮助他在架构规划及管理过程中站在更高的角度去看待问题,历史发展原因有很多已成体系的架构理论,TOGAF是近年来比较接地气的,受到了政府和银 ...

  8. metasploitable实践

    使用Fimap和metasploitable2文件包含漏洞测试 fimap 首先查看msf已经存在的漏洞: root@kali:~# fimap -u 'http://192.168.136.130/ ...

  9. ZOJ2208 To and Fro 2017-04-16 19:30 45人阅读 评论(0) 收藏

    To and Fro Time Limit: 2 Seconds      Memory Limit: 65536 KB Mo and Larry have devised a way of encr ...

  10. Sourcetree拉取推送问题

    以下sourcetree使用时出现的问题:可以暂时用删除passwd文件解决 拉取时账号密码输入错误 已经有一个远程仓库了,但是需要拉取另一个远程仓库时,提示URL 进入 C:\Users\用户名\A ...