leetcode-268-Missing Number(异或)
题目描述:
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(异或)的更多相关文章
- leetcode 268 Missing Number(异或运算的应用)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Java [Leetcode 268]Missing Number
题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- [LeetCode] 268. Missing Number 缺失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 33. leetcode 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- LeetCode 268. Missing Number (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 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 ...
- 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 ...
- Leetcode 268 Missing Number 位运算
题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
随机推荐
- Android SDK Manager检查更新时遇到Failed to fetch URL xxxxxxx reason: Connection to xxxxxx的错误的解决办法!
首先说明的是这个问题并不是每个人都存在,但是我和我的一个同学都有这种情况,所以我同学百度了一下,找出了解决办法. 问题描述: 使用Android SDK Manager检查在线更新,提示以下错误: & ...
- 最多有k个不同字符的最长子字符串 · Longest Substring with at Most k Distinct Characters(没提交)
[抄题]: 给定一个字符串,找到最多有k个不同字符的最长子字符串.eg:eceba, k = 3, return eceb [暴力解法]: 时间分析: 空间分析: [思维问题]: 怎么想到两根指针的: ...
- Golang之struct
1.用来定义复杂数据结构 2.struct里面可以包含多个字段(属性) 3.struct类型可以定义方法,注意和函数的区分. 4.struct类型是值类型 5.struct类型可以嵌套 6.Go语言没 ...
- Java 设计模式系列(十四)命令模式(Command)
Java 设计模式系列(十四)命令模式(Command) 命令模式把一个请求或者操作封装到一个对象中.命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复 ...
- jdbc和Java中的日期问题
JDBC中的日期Java.sql.Date 是继承自Java中的Java.util.Date,在实现插入的时候可以 将Java.util.Date类型的时间转换成毫秒数,date.getTime(), ...
- 访问localhost文件下的testmysql.php文件报Not Found
但是访问localhost:8081/index.php没有报该错误,页面显示success,并没有显示wamp的主页 出错原因:Apache和php没关联好,修改一下Apache的httpd.con ...
- 团队-Forward-团队一阶段互评
学号:2015035107105得分:4原因:代码不规范,有一些错误,需要我们的帮助. 学号:2015035107109得分:7原因:与队员沟通少,代码衔接有问题. 学号:2015035107113得 ...
- POJ1273&&Hdu1532 Drainage Ditches(最大流dinic) 2017-02-11 16:28 54人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Python学习-7.Python的循环语句-for语句
Python中循环可以使用for语句来实现 list = ['Tom','Lucy','Mary'] for name in list: print(name) 则将会依次输出Tom Lucy Mar ...
- ------------------java collection 集合学习 ----小白学习笔记,,有错,请指出谢谢
<!doctype html>java对象集合学习记录 figure:first-child { margin-top: -20px; } #write ol, #write ul { p ...