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.
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的和s,再求数组的和s2,s-s2就是少的那个数。
由于计算机中异或运算比加法运算快。而且这道题正好可以用异或的性质来解决。
两个相同的数异或结果是0.
0和其他数异或结果还是那个数。
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n=nums.size();
int s=;
for(int i=;i<=n;i++){
s^=i;
}
int s2=nums[];
for(int i=;i<n;i++){
s2^=nums[i];
}
return s^s2;
}
};
leetcode 268 Missing Number(异或运算的应用)的更多相关文章
- 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 位运算
题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...
- 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 - 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 OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
随机推荐
- python学习 04 函数参数
1.参数可以传递元组,但是要加* 2.参数可以传递字典,但是要加**
- Oracle Data Provider for .NET的使用(二)-驱动更换与注意事项
上篇说过了ODP的安装与配置 ,但是个人比较喜欢托管类型的,毕竟非托管类型的,因为考虑到会有用户或者是服务器或者是开发人员有32位的机器,就要强制编译平台平台为32位,只因为这个驱动,有点让人不愉快了 ...
- pycharm中不能安装bs4的解决方案
首先:什么Beautiful Soup? Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.B ...
- windows常用dos命令
常用命令: d: 回车 磁盘切换 dir: 查看该目录下所有的文件和文件夹: md: 创建文件加 rd: 删除目录 cd: 进入指定的目录 cd..:回退到上级目录 cd\ :回退到根目录 de ...
- "活在未来" VS “活在当下”(通向财富自由学习笔记六)
之前读过一些灵修类的书籍,<遇见未知的自己>.<当下的力量>等都在告诉我们活在当下很重要,这里笑来老师提出了一个问题,是活在当下重要呢?还是活在未来?,笑来老师给出了很好的答案 ...
- web.xml配置整理
虽然是做web开发,但是web中的很多配置有的时候却不是很清楚,只是知道怎么配置,于是就把在网上看到各种关于web.xml的东西整理一下: web.xml中url-pattern的3种写法 1完全匹配 ...
- 【BZOJ4009】[HNOI2015]接水果 DFS序+整体二分+扫描线+树状数组
[BZOJ4009][HNOI2015]接水果 Description 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果.由于她已经DT FC 了The big black, ...
- 多语言中的“默认语言”设置
最近在搞一个多语言的东西,打算如果用户是中文环境就显示中文,其他任何非中文环境就显示英文.换句话说,把默认语言设置成英文. 不过因为VS是中文的,发现即使默认资源文件是英文(AppResource.r ...
- why factory pattern and when to use factory pattern
1 factory pattern本质上就是对对象创建进行抽象 抽象的好处是显然的,可以方便用户去获取对象. 2 使用factory pattern的时机 第一,当一个对象的创建依赖于其它很多对象的时 ...
- Js自定义异常
function MyError(msg){ this.name="MyError"; this.message = msg || "自定义异常的默认消息";} ...