Leetcode86: Missing Number
2015-10-08 21:23
162 查看
Given an array containing n distinct numbers taken from
find the one that is missing from the array.
For example,
Given nums =
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
根据异或的特性,对于一个数,异或自己是0,异或0是自己,所以我们把0-n对着给定数组异或一遍,结果就是缺失的数。
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?
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
根据异或的特性,对于一个数,异或自己是0,异或0是自己,所以我们把0-n对着给定数组异或一遍,结果就是缺失的数。
class Solution { public: int missingNumber(vector<int>& nums) { int res = 0; int i ; for(i = 0; i < nums.size(); i++) { res ^= i^nums[i]; } res ^= i; return res; } };
相关文章推荐
- SSL 证书申请(居然还可以在淘宝上购买)
- Merge Two Sorted Lists & Remove Nth Node From End of List
- android图片模糊的效果
- [Poj1004]Financial Management
- LP/QP/QCQP/SOCP/SDP/CCCP等系列优化方法的比较
- 【July程序员编程艺术】之字符串左旋
- python脚本执行Scapy出现IPv6警告WARNING解决办法
- HDU5496--Beauty of Sequence(水题)
- 响应式布局
- 剑指offer—字符流中第一个不重复的字符
- Java中的集合类型的迭代器(Iterator)
- sqlite学习
- Tomcat6环境JBPM4.4报错:java.lang.ClassNotFoundException: de.odysseus.el.util.SimpleResolver
- 内核学习-中断级别,分页内存,用户模式以及内核模式
- Android混淆打包后openFileChooser无法使用找不到的解决方法
- linux 编译内核[scripts/kconfig/dochecklxdialog] 错误
- netstat命令
- 黑马程序员——OC基础---继承
- 一个简单的网站访问过程
- LBP(local binary pattern)