【leetcode79】Single Number III
题目描述:
给定一个数组,里面只有两个数组,只是出现一次,其余的数字都是出现两次,找出这个两个数字,数组形式输出
原文描述:
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?
思路:
考虑使用位运算
-那数组分为两组,每个组只有一个出现一次的数字,单独进行异或处理,考虑用全部异或的方式假设得到异或的结果是A,由于两个数字不同。A肯定有一个位是1,找到那个位,然后用来把这个数组分为两组
- 其中一个组的异或结果是B,输出【B,A ^ B】
 
代码:
public class Solution {
    public int[] singleNumber(int[] nums) {
        int xOne = 0;
        for (int x : nums) {
            xOne ^= x;
        }
        // 获取第一个位1的索引
        int k = 0;
        for (k = 0; k < Integer.SIZE; ++ k) {
            if (((xOne >>> k) & 1) == 1) {
                break;
            }
        }
        int xTwo = 0;
        for (int x : nums) {
            if (((x >>> k) & 1) == 1) {
                xTwo ^= x;
            }
        }
        return new int[] {xTwo, xOne ^ xTwo};
    }
}
更多的leetcode的经典算法,查看我的leetcode专栏,链接如下:
我的微信二维码如下,欢迎交流讨论
欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!
微信订阅号二维码如下:
【leetcode79】Single Number III的更多相关文章
- 【LeetCode】Single Number I & II & III
		
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
 - 【leetcode78】Single Number II
		
题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...
 - 【leetcode】Single Number && Single Number II(ORZ 位运算)
		
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
 - 【03_136】Single Number
		
感谢:http://www.cnblogs.com/changchengxiao/p/3413294.html Single Number Total Accepted: 103007 Total S ...
 - 【题解】【位操作】【Leetcode】Single Number II
		
Given an array of integers, every element appears three times except for one. Find that single one. ...
 - 【Leetcode】 -  Single Number II
		
Problem Discription: Suppose the array A has n items in which all of the numbers apear 3 times excep ...
 - 【Leetcode】【Medium】Single Number II
		
Given an array of integers, every element appears three times except for one. Find that single one. ...
 - 【leetcode】Single Number II (medium) ★  自己没做出来....
		
Given an array of integers, every element appears three times except for one. Find that single one. ...
 - 【leetcode】Single Number (Medium) ☆
		
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
 
随机推荐
- jdk和tomcat配置
			
1.一次成功的JAVA环境变量配置,必须要配置一下三个系统变量:JAVA_HOME(变量值为JDK的路径),PATH(变量值:%JAVA_HOME%\bin;),CLASS_PATH(变量值为JDK中 ...
 - python:浅析python 中__name__ = '__main__' 的作用(转载)
			
每次看文章的源码时,Python的主程序中都会看到开头有这个,查了一下作用:https://www.cnblogs.com/alan-babyblog/p/5147770.html. 讲的很详细,就直 ...
 - ScalaPB(1): using protobuf in akka
			
任何类型的实例作为消息在两端独立系统的机器之间进行传递时必须经过序列化/反序列化serialize/deserialize处理过程.假设以下场景:在一个网络里有两台连接的服务器,它们分别部署了独立的a ...
 - ng-book札记——Angular工作方式
			
Angular应用由组件(Component)构成.它与AngularJS中的指令相似(directive). 应用 一个Angular应用本质上是一个组件树.在组件树的顶层,最上级的组件即是应用本身 ...
 - CF | Alyona and Numbers
			
After finishing eating her bun, Alyona came up with two integers n and m. She decided to write down ...
 - Docker标准化开发测试和生产环境
			
对于大部分企业来说,搭建 PaaS 既没有那个精力,也没那个必要,用 Docker 做个人的 sandbox 用处又小了点. 可以用 Docker 来标准化开发.测试.生产环境. Docker 占用资 ...
 - Oracle数据库常用命令记录
			
1.Sql建表 CREATE TABLE AAABBBCCCDDD( ID ) primary key, AAAAAAAA ) not NULL, BBBBBBBB ), CCCCCCCC ), DD ...
 - 团队项目必备神器——自定义Lint
			
Lint 在android studio中内置了大概200个左右的lint检查,比如定义变量未使用,直接Handler报内存泄漏提醒,时时刻刻在监督着我们的代码.自己定制了一些Lint规则,项目开源在 ...
 - ROS机器人程序设计(原书第2版)补充资料 (零) 源代码、资料和印刷错误修订等 2017年01月01日更新
			
ROS机器人程序设计(原书第2版)补充资料 (零) 源代码等 ROS官网 版)部分内容修订 页:第1行,删去$ 页:第6行,float64 y 前面加一个空格 页:中间创建主题:下面程序不用换行,(& ...
 - JBOSS EAP 6 系列六 公共模块的jar配置到jboss的modules详细配置
			
公司项目中遇到并要解决的问题 1:原则上除了自己写的代码之外,公共的jar不应该都在打包的时候打包到ear里面,这样的话包太大,也不符合的分层的逻辑,在jboss容器内部,每个ear的包重复jar都会 ...