Leetcode 260 Single Number III 亦或
在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次)
同样用亦或来解决(参考编程之美的1.5)
先去取出总亦或值
然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1]
a&(-a)就是计算出这个数,可以参考树状数组。
最后就是注意(nums[i] & a) == 0要加()。注意符号运算优先级
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int a = ;
for(int i = ; i<nums.size(); ++i){
a ^= nums[i];
}
vector<int> ans(,);
a &= (-a);
for(int i = ; i<nums.size(); ++i){
ans[ (nums[i] & a) == ] ^= nums[i];
}
return ans;
}
};
Leetcode 260 Single Number III 亦或的更多相关文章
- LeetCode 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- [LeetCode] 260. Single Number III 单独数 III
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode#260]Single Number III
Problem: Given an array of numbers nums, in which exactly two elements appear only once and all the ...
- Java [Leetcode 260]Single Number III
题目描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...
- LeetCode 260 Single Number III 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] 260. Single Number III(位操作)
传送门 Description Given an array of numbers nums, in which exactly two elements appear only once and a ...
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- 【刷题-LeeetCode】260. Single Number III
Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...
随机推荐
- swift项目第十天:网络请求工具类的封装
import UIKit /* 必须先导入头文件:import AFNetworking */ import AFNetworking //MARK:-0:定义枚举:以枚举定义请求网络的get和pos ...
- 【7001】n阶法雷序列
Time Limit: 10 second Memory Limit: 2 MB 问题描述 对任意给定的一个自然数n(n<=100),将分母小于等于n的不可约的真分数按上升的次序排序, ...
- css3-10 如何使用滚动条
css3-10 如何使用滚动条 一.总结 一句话总结:给设置了宽高的块标签使用,直接将overflow属性写到style里面即可. 1.滚动条的使用对象时谁? 一般是div,当div比较小(设置了宽高 ...
- php实现 称砝码(背包)
php实现 称砝码(背包) 一.总结 一句话总结: 1.dp的实质是什么? 刷表啊,用空间换时间 把表画出来会做得更快 13 //动态规划就是一个表 14 //至于这个表的更新就是上面层的表更新下面层 ...
- [AngularFire2] Building an Authentication Observable Data Service
After successfully login, we want something help to check whether user has already login or not. And ...
- ArcGIS二次开发入门(一)
作者:朱金灿 来源:http://blog.csdn.net/clever101 公司培训ArcGIS二次开发(C#作为开发语言),自己回家动手实践了一下. 1. 首先是二次开发环境的安装(AE 10 ...
- MRTG Monitoring with ESXi Hosted Guest Return ‘interface is commented * has no ifSpeed property’
MRTG Monitoring with ESXi Hosted Guest Return ‘interface is commented * has no ifSpeed property’ Rec ...
- [tmux] Handle history in tmux sessions
In this lesson, we'll look at how to manage your history between tmux sessions, and ensure that your ...
- jquery平滑滚动页面
滚动到顶部 $('.scroll_top').click(function(){$('html,body').animate({scrollTop: '0px'}, 800);}); 滚动到指定位置 ...
- SEO那些事:一句代码一键分享网站
这是很久以前就已经写过的笔记了,有一个习惯,每次遇到一个问题,都会进行百度,然后把解决问题的关键点记录下来,有人问我,为什么更新频率如此之快,大部分都是从前积累的知识点. 其实每天工作所涉及的知识点都 ...