Single Number III——LeetCode
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?
题目大意:一个数组,有两个元素出现了一次,其余元素都出现两次,让用线性时间复杂度和常量空间复杂度找出这两个数。
解题思路:考虑位操作,对所有元素做异或操作,那么最后得到的值就是要求的两个元素异或得到的,那么找出其中为1的某一位,说明这一位两个数一个为0,一个为1,以这一位为标准,把数组的元素分为两组A、B,那么要求的两个元素肯定是一个在A组,一个在B组,而对这两组各自做异或操作,就可以得到两个数就是要求的。
public class SingleNumberIII {
public static void main(String[] args) {
int[] res = singleNumber(new int[]{1, 2, 1, 3, 5, 2});
System.out.println(Arrays.toString(res));
}
public static int[] singleNumber(int[] nums) {
int[] res = new int[2];
if (nums == null || nums.length == 0) {
return res;
}
for (int i = 0; i < nums.length; i++) {
res[0] ^= nums[i];
}
int pos = 0;
for (int i = 0; i < 32; i++) {
int offset = 1 << i;
if ((res[0] & (offset)) == offset) {
pos = i;
break;
}
}
res[0] = 0;
int mask = 1 << pos;
for (int i = 0; i < nums.length; i++) {
if ((mask & nums[i]) == mask) {
res[0] ^= nums[i];
} else {
res[1] ^= nums[i];
}
}
return res;
}
}
Single Number III——LeetCode的更多相关文章
- Single Number III leetcode java
问题描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...
- 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 ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
随机推荐
- codevs 1170 双栈排序
/* 好题啊 好题啊 而然还是看了一眼题解啊 有那么一点思路 但是离写出代码还很远 考虑必须分开放倒两个栈里的情况 即存在i<j<k 有 a[k]<a[i]<a[j] 这里RM ...
- 控件AutoCompleteTextView实现动态匹配输入内容的一种输入框
<AutoCompleteTextView android:layout_width="match_parent" android:layout_height="w ...
- 关于webservice不支持方法重载的解决办法
今天在写WebService时,出现了这样的错误: Count(Int32, Int32) 和 Count(Int32) 同时使用消息名称“Count”.使用 WebMethod 自定义特性的 Mes ...
- 网站如何防Session冒名顶替和cookie防篡改
做网站难免要面对安全性的问题,诸如sql注入拉,cookie冒名拉,等等,sql注入算是老生常谈,翻翻旧账有不少优秀的帖子在说明这个问题,所以我们来说说Session冒名顶替的风险以及应对的办法. 首 ...
- vs2010安装路径解决不能修改的方法
环境:win7 64位 解决:网上说需要卸载以下4项 Microsoft Visual Studio Tools for Applications 2.0 - ENU Microsoft Visual ...
- SGU 156. Strange Graph(欧拉路)
时间限制:0.25s 空间限制:6M 题目描述 让我们想象一个无向图G=<V,E>.如果边(u,v)在边集E中,那么我们就说两个顶点u和v是邻接点.在这种情况下,我们也说u是v的一个邻接点 ...
- POJ 1830.开关问题(高斯消元)
题目链接 Solutin: 将每个开关使用的情况当成未知数,如果开关i能影响到开关j,那么系数矩阵A[j][i]的系数为1. 每个开关增广矩阵的值是开关k的初状态异或开关k的目标状态,这个应该很容易想 ...
- 移动端版本兼容js
移动端版本兼容js <!--移动端版本兼容 --> <script type="text/javascript"> var phoneWidth = par ...
- php笔记之GD库图片创建/简单验证码
燕十八 公益PHP培训 课堂地址:YY频道88354001 学习社区:www.zixue.it php画图:比如说验证码,缩略图,加水印都要用到GD库,所以要开启gd2库,才能用 首先找到php.in ...
- mysql中显示方式的切换
1. mysql中如果使用\G,则':'不用写.如果\G后面跟':'则会报"error:no query specified"错误.请知晓. 2. mysql在登陆时,mysql ...