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 ...
随机推荐
- 多媒体开发之--- rtsp 中的H264 编码+打包+解码相关知识es、pes、ts...
1)ES流(Elementary Stream): 也叫基本码流,包含视频.音频或数据的连续码流. 2)PES流(Packet Elementary Stream): 也叫打包的基本码流, 是将基本的 ...
- java 经典范例
使用for 循环输出空心菱形 package 开阳; import java.util.Scanner; public class image { public static void main(St ...
- 使用 SourceTree 遇到冲突的解决方法
首先,更新代码之前先 git stash ,然后 git pull ,再 git stash pop 这时候如果本地改的代码跟线上的冲突了,就报错了.那么就需要手动解决冲突. 打开存在冲突的文件,会看 ...
- hdu 5881 Tea (2016 acm 青岛网络赛)
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5881 Tea Time Limit: 3000/1000 MS (Java/Others) Me ...
- 鸟哥的Linux私房菜-----6、文件与文件夹管理
- 【BZOJ3316】JC loves Mkk 分数规划+单调队列
[BZOJ3316]JC loves Mkk Description Input 第1行,包含三个整数.n,L,R.第2行n个数,代表a[1..n]. Output 仅1行,表示询问答案.如果答案是整 ...
- 【BZOJ3926】[Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机
[BZOJ3926][Zjoi2015]诸神眷顾的幻想乡 Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝 ...
- City Game(最大子矩阵)
Bob is a strategy game programming specialist. In his new city building game the gaming environment ...
- wcf读取message内容
private string MessageToString(ref Message message) { WebContentFormat messageFormat = this.GetMessa ...
- 九度OJ 1255:骰子点数概率 (递归、DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:316 解决:29 题目描述: 把n个骰子扔在地上,所有骰子朝上一面的点数之和为S.输入n,打印出S的所有可能的值出现的概率. 输入: 输入包 ...