Lintcode: Single Number III
Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example
Given [1,2,2,3,4,4,5,3] return 1 and 5 Challenge
O(n) time, O(1) extra space.
利用bitwise XOR的特点,n个数(0或1),如果1的个数为奇数,则n个数bitwise XOR结果为1,否则为0
先将所有的数异或,得到的将是x和y以后之后的值n。 找到这个数n的为1的某一位(为了方便就取最右边为1的一位, n & ~(n-1),再将这一位为1的数异或,其余的数异或,得到的就是x和y的值。
public class Solution {
/**
* @param A : An integer array
* @return : Two integers
*/
public List<Integer> singleNumberIII(int[] A) {
// write your code here
ArrayList<Integer> res = new ArrayList<Integer>();
res.add(0);
res.add(0);
int n = 0;
for (int elem : A) {
n ^= elem;
}
n = n & (~(n-1));
for (int elem : A) {
if ((elem & n) != 0) {
res.set(0, res.get(0)^elem);
}
else res.set(1, res.get(1)^elem);
}
return res;
}
}
Lintcode: Single Number III的更多相关文章
- Single Number III(LintCode)
Single Number III Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example G ...
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- 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)
- 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 ...
- [LintCode] Single Number 单独的数字
Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Have you met this question in ...
- LeetCode Single Number III
原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...
随机推荐
- MVC设计模式
随着Web应用的商业逻辑包含逐渐复杂的公式分析计算.决策支持等,使客户机越 来越不堪重负,因此将系统的商业分离出来.单独形成一部分,这样三层结构产生了. 其中‘层’是逻辑上的划分. 三层体系结构是将整 ...
- Decoder with 3 Inputs and 2 3 = 8 Outputs
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION
- SQL注入攻击和防御
部分整理... 什么是SQL注入? 简单的例子, 对于一个购物网站,可以允许搜索,price小于某值的商品 这个值用户是可以输入的,比如,100 但是对于用户,如果输入,100' OR '1'=' ...
- Windows与Linux共享文件夹互相访问
[原文] 首先安装并配置软件samba [html] view plain copy sudo yum install samba samba-client vim /etc/samba/smb.c ...
- (转)常用的js设计模式
模式是解决或者避免一些问题的方案. 在JavaScript中,会用到一些常用的编码模式.下面就列出了一些常用的JavaScript编码模式,有的模式是为了解决特定的问题,有的则是帮助我们避免一些Jav ...
- ADO.NET实体数据模型使用探索1
今天研究了下ADO.NET实体数据模型,想写个关于两张有外键关系的增改删查,以此来稍增加点难度. 编程环境:vs2010+sql2005 1.在SQL2005下建立三张表:学生信息表Student(S ...
- php安装xcache (5.4)
安装环境centOS6.3APACHE:apache-2.4.4PHP:5.4.13 1.安装xchache: 代码如下: # wget http://xcache.lighttpd.net/pub/ ...
- 【Java 基础篇】【第五课】类的构造函数
Java 也有自己的构造函数,如同c++一样有两个特征: 1.构造函数的名字和类的名字相同 2.构造函数没有返回值 下面来看一下这个例子: public class test { public sta ...
- C++ Primer Pluse_6_课后题
#include <iostream> #include <cctype> #include <array> #include <string> #in ...
- ios证书
内容提要: 安装app时提示 “无法下载应用,此时无法安装“XXX””.我遇到过多次是由于ios的app出现证书问题.本篇文章讲解用ios证书制作过程,以及每个步骤的解释. 正文: Xcode签名至少 ...