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 ...
随机推荐
- LDD3之并发和竞态-completion(完毕量)的学习和验证
LDD3之并发和竞态-completion(完毕量)的学习和验证 首先说下測试环境: Linux2.6.32.2 Mini2440开发板 一開始难以理解书上的书面语言,这里<linux中同步样例 ...
- 11、V4L2摄像头获取单幅图片测试程序
#根据网上常见的一个测试程序修改而来 by rockie cheng#include <stdio.h>#include <stdlib.h>#include <stri ...
- Java中使用org.json和json-lib解析JSON
文章目录 [隐藏] 一.JavaProject中org.json解析JSON 1.JSON的org.son-api下载 1)JSON网址 2)JSON的java解析org.json-api网址 3) ...
- Genymotion加入模拟器时报“Unable to create virtual device,Server returned HTTP status code 0”
今天也遇到这个问题,算是对这个文章的一点补充 打开图中这个文件 C:\Users\xxx\AppData\Local\Genymobile 搜索 [downloadFile] 找到这个一串URL ht ...
- codeforces 571B--Minimization(贪心+dp)
D. Minimization time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Multivariate Linear Regression
Multiple Features Linear regression with multiple variables is also known as "multivariate line ...
- A Guide to Python's Magic Methods
Book Source:[https://rszalski.github.io/magicmethods/] magic methods: 名称前后有双下划线的方法 构造函数和初始化 初始化类实例时, ...
- position:relative和position:absolute的差别及使用方法
这几天在做项目时遇到做选项卡的功能时,标题和内容区域的背景颜色不同.且须要选到当前标题时,此标题以下会出现下边框及小三角边框,这样就会超出标题背景颜色需覆盖以下内容区域.这时就须要用到potition ...
- iOS开发AFN使用二:AFN文件下载与文件上传
#import "ViewController.h" #import "AFNetworking.h" @interface ViewController () ...
- jquery-11 如何制作鼠标右键菜单
jquery-11 如何制作鼠标右键菜单 一.总结 一句话总结:核心原理:找到右键菜单事件contextmenu,return false去掉默认事件,然后判断用户是否点的右键,然后在鼠标的位置显示菜 ...