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 ...
随机推荐
- 手把手教你----MyEclipse中 配置 Tomcat
电脑上配置Tomcatserver 安装Tomcat并配置环境变量 測试是否配置成功 MyEclipse中配置Tomcat 想要开发Java Web的程序.首先在MyEclipse中必须配置Tomca ...
- [PostgreSQL] Ensure Uniqueness in Postgres
Let’s say we have a bank. Our bank wants to give each account for each user a unique name, for insta ...
- Maven项目中mvn clean后找不到測试类问题
在Maven项目中进行单元測试,但mvn clean后又一次mvn install项目,再次进行单元測试.会有下面的错误. <span style="font-family:KaiTi ...
- POJ 题目2506Tiling(大数)
Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8128 Accepted: 3941 Descriptio ...
- js 99乘法表
哈哈哈,笑死我了,突然怀念学习时代,撸了一个乘法表 for(let a=1;a<10;a++){let str = ''; for(let b=1;b<10;b++){ str = str ...
- 最简单的基于FFmpeg的AVUtil样例 (AVLog, AVOption等)
本文的演示样例程序记录了FFmpeg的libavutil中几种工具函数的用法: AVLog:日志输出AVOption (AVClass):选项设置AVDictionary:键值对存储ParseUtil ...
- 设置UIButton的文字显示位置、字体的大小、字体的颜色
btn.frame = CGRectMake(x, y, width, height); [btn setTitle: @"search" forState: UIControlS ...
- js判断是否微信客户端
上周接到个需求,需求是这样的:用户扫一扫二维码会产生一个链接,该链接会向后端发送个请求,返回一个 apk 的下载地址,用户点击下载按钮可以下载此 apk.然后就发生了问题,经过测试,发现用微信扫一扫打 ...
- ios开发transform属性
#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...
- Powerful Bash-style command line editing for cmd.exe
https://mridgers.github.io/clink/ Clink Powerful Bash-style command line editing for cmd.exe Downloa ...