[LeetCode] 260. Single Number III(位操作)
Description
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
Note:
- The order of the result is not important. So in the above example, [5, 3] is also correct.
- Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
思路
题意:给定一个数组,其中除了两个数出现一次外,其他都出现两次,要求在时间复杂度为O(n)空间复杂度为O(1)的条件下找出这两个数。
题解:按照异或的思想,最后可以得到这两个出现一次的数的异或值,那么我们只需对这个异或值lowbit操作,得到其最低位为1的位在哪,然后根据lowbit后得到的值,将数据分为两堆,那么可以知道的是这两个出现一次的数肯定分布在不同的两堆中(因为是对这这两个数的异或值lowbit操作,因此其为1的最低二进制位,这两个数肯定一个数0一个是1),那么问题转化为子问题,有一堆数,出了一个数出现一次外,其他数都出现两次。
class Solution {
public:
//26ms
vector<int> singleNumber(vector<int>& nums) {
int diff = 0,one = 0,two = 0;
vector<int>res;
for (unsigned int i = 0;i < nums.size();i++){
diff ^= nums[i];
}
diff &= (-diff); //lowbit操作
for (unsigned int i = 0;i < nums.size();i++){
if (nums[i] & diff) one ^= nums[i];
else two ^= nums[i];
}
res.push_back(one);
res.push_back(two);
return res;
}
};
[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 亦或
在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次) 同样用亦或来解决(参考编程之美的1.5) 先去取出总亦或值 然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1] a&am ...
- 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 ...
随机推荐
- 行人重识别(ReID) ——技术实现及应用场景
导读 跨镜追踪(Person Re-Identification,简称 ReID)技术是现在计算机视觉研究的热门方向,主要解决跨摄像头跨场景下行人的识别与检索.该技术能够根据行人的穿着.体态.发型等信 ...
- 基于 VirtualApp 结合 whale hook框架实现hook第三方应用
要点 1. whale hook framework 使用示例: 2. 参考项目:VirtualHook: 3. 按照 VirtualHook 修改 VirtualApp: 4. 编写 hook pl ...
- jQuery学习总结02-属性
1.attr(name|properties|key,value|fn) 说明:设置和返回被选元素的属性值 示例: 参数: name(属性名称) string properties(作为属性的'名/值 ...
- Sql 统计一个表有多少列
SELECT COUNT(syscolumns.name) FROM syscolumns , sysobjects WHERE syscolumns.id = sysobjects.id AND s ...
- 在数据库中分析sql执行性能
SET STATISTICS PROFILE ON SET STATISTICS IO ON SET STATISTICS TIME ON GO /*--SQL脚本开始*/ SELECT * FROM ...
- 18-基于双TMS320C6678 DSP的3U VPX的信号处理平台
基于双TMS320C6678 DSP的3U VPX的信号处理平台 一.板卡概述 板卡由我公司自主研发,基于3U VPX架构,处理板包含两片TI DSP TMS320C6678芯片:一片Xilinx公司 ...
- Spring_搭建过程中遇到的问题
先看一下问题: 1.在web.xml中配置Spring 加载Spring mvc的时候配置如下: <!--配置SpringMVC的前端控制器--> <servlet> < ...
- python基础模块,包
#import cal,time #导入模块名可以看作导入一个变量 #from cal import add # from cal import *#引入所有变量 *代表所有 占内存 不推荐 # # ...
- join 按两个文件的相同字段合并
1.命令功能 join对每一对具相同内容的输入行,合并为一行输出.默认情况是把输入的第一个字段作为连接字段,字段间用空格隔开. 2.语法格式 join option file1 file2 jo ...
- Sumdiv(约数和问题)
题目地址 看到这题的题解,大佬都说是小学奥数,蔡得我不敢鸡声. 求 \(a^b\) 所有的约数之和 mod \(9901\) \((1<=a,b<=5*10^7)\) 题解 做这道题,我还 ...