Single Number III leetcode java
问题描述:
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?
提示:bit manipulation
一、位运算
//参照single number 的方法,将所有数异或运算,得到result为两个single number的异或;
//找到result中最低位为1的那一位,是两个single number不同的地方,根据这个bit,可以将nums数组中的数分为A、B两组;
//然后分别在A和B中寻找single number即可
public static int[] singleNumber_Bit(int[] nums){ int result = 0;
for(int i = 0; i < nums.length; i++){
result = result ^ nums[i];
}
int[] res = new int[2];
//找到result二进制表示中最右侧的1
int pos = result & ( ~ (result - 1 )); //统计一个int型整数的二进制表示中有多少个1,可以采用 n = n & (n - 1);来从右到左逐个统计1的个数
for(int i = 0 ; i < nums.length; i++){ //将nums分为A B两组来分别求single number
if((pos & nums[i]) != 0){
res[0] = res[0] ^ nums[i];
} else {
res[1] = res[1] ^ nums[i];
}
}
return res;
}
Single Number III leetcode java的更多相关文章
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
- Single Number III——LeetCode
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- Single Number III - LeetCode
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- 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 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- Single Number III(LintCode)
Single Number III Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example G ...
- LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III
136. Single Number Given an array of integers, every element appears twice except for one. Find that ...
- 【刷题-LeeetCode】260. Single Number III
Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...
随机推荐
- Spring整合Redis&JSON序列化&Spring/Web项目部署相关
几种JSON框架用法和效率对比: https://blog.csdn.net/sisyphus_z/article/details/53333925 https://blog.csdn.net/wei ...
- div包裹页面后多余部分没有显示,也没滚动条 overflow 属性设置
今天弄个div套着一个页面结果那个页面超出范围后页面没有滚动条可以滚动浏览下面的内容,原来是设置了overflow的hidden属性 visible 默认值.内容不会被修剪,会呈现在元素框之外. hi ...
- (zhuan) Paper Collection of Multi-Agent Reinforcement Learning (MARL)
this blog from: https://github.com/LantaoYu/MARL-Papers Paper Collection of Multi-Agent Reinforcemen ...
- OpenCV学习一《Linux下安装OpenCV》
第一步:安装源码前先安装好需要的第三⽅方环境 需要的编译环境■ [compiler] sudo apt-get install build-essential # 注释说明 64位ubuntu在安装 ...
- ETCD原理
etcd:从应用场景到实现原理的全方位解读 从etcd的架构开始,深入到源码中解析etcd 1 架构 从etcd的架构图中我们可以看到,etcd主要分为四个部分. HTTP Server: 用于处理用 ...
- 小程序之image图片实现宽度100%,高度自适应
哇 今天搞了半天 图片一直变形啊啊啊啊 宽度100% 高度给100% 给auto 完全不管用啊啊啊啊 然后最后最终!!!! 首先我们要给我们的图片一个100%的宽度!让它自适应!! .g ...
- _itemmod_gem_limit
该表可以控制特定宝石的数量上限,即使玩家多插了宝石,也不会有相应效果 `entry` 宝石ID `limitCount`上限值 `comment`备注
- _spellmod_leech_spell
comment 备注 spell 技能ID,玩家释放该技能时附带吸血效果 meetAura 产生吸血效果需要满足的光环ID,比如做一个空的光环,为寒冰箭吸血光环,则有些光环时候,寒冰箭会附带吸血效 ...
- 性能跃升50%!解密自主研发的金融级分布式关系数据库OceanBase 2.0
小蚂蚁说: 相信大家对蚂蚁金服自主研发的金融级分布式关系数据库OceanBase的故事不再陌生了.在刚刚过去的2018年天猫双11中,成交额2135亿再次创造了新纪录,而支撑今年双11的支付宝核心链路 ...
- P1547 Out of Hay
传送门 练习 只是一个最小生成树的水题,拿来练练模板 AC代码: #include<iostream> #include<cstdio> #include<alg ...