[LeetCode]Missing Number
2016-03-26 14:08
375 查看
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?
首先对0~ n内数字做异或,在对数组内数字做异或。等价与在Single Number 1,在一堆重复数字中找1个。
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?
首先对0~ n内数字做异或,在对数组内数字做异或。等价与在Single Number 1,在一堆重复数字中找1个。
class Solution { public: int missingNumber(vector<int>& nums) { int ret = 0; for(int i=0; i<=nums.size(); ++i){ ret ^= i; } for(int i=0; i<nums.size(); ++i){ ret ^= nums[i]; } return ret; } };
相关文章推荐
- 如何让ARM板启动的时候就运行自己的QT程序
- 来CSDN写博客的一些原因
- 底层const
- 双击文件夹新建一个窗口问题
- 【Android】 图片编辑:创建圆角图片
- java13天
- 【Machine Learning实验5】SVM实验
- 母牛生小牛
- 传球问题
- 付款问题
- 终生受益的职业规划(转自惠普前总裁)
- Linux 常用操作
- VC与JavaScript交互(三) ———— JS调用C++
- 精简ID号码
- [Machine learning 实验4]linear programming
- 二进制字符串匹配
- Ant_的最完整build.xml解释
- delphi主i窗口中实现多页面管理效果
- ”爆打“3-25号站立会议
- vi/vim 相关笔记