原题链接在这里:https://leetcode.com/problems/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.

Example:

Input:  [1,2,1,3,2,5]
Output: [3,5]

Note:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

题解:

思路是逐个xor, 得到的结果是两个出线单次的数字a 和 b 的异或,axorb. a 与 b 不相同,所以a, b 取 xor肯定不为0,通过axorb $ (-axorb)找到 axorb中从右往左第一个不为0的digit.

通过这个digit把原来的nums 数组分成两类,一类这个digit上是0一类这个digit上是1. res[0] 来 xor 第一类, res[1] 来 xor 第二类.

Note: 位运算级别很低,需要用() 保护。

Time Complexity: O(nums.length).

Space: O(1).

AC Java:

 public class Solution {
public int[] singleNumber(int[] nums) {
int [] res = {0,0};
if(nums == null || nums.length == 0){
return res;
}
//internal result a XOR b
int axorb = 0;
for(int i = 0; i<nums.length; i++){
axorb ^= nums[i];
} //from right to left, mark first digit that a and b are different
//通过这个mark把 原nums 数组分成两类,一类这个digit上是1,一类这个digit上是0.
int mark = axorb & (-axorb);
for(int i = 0; i<nums.length; i++){
if((nums[i] & mark) != 0){
res[0] ^= nums[i];
}else{
res[1] ^= nums[i];
}
}
return res;
}
}

类似Single NumberSingle Number II.

LeetCode Single Number III的更多相关文章

  1. [LeetCode] Single Number III 单独的数字之三

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  2. [LeetCode] Single Number III ( a New Questions Added today)

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  3. LeetCode——Single Number III

    Description: Given an array of numbers nums, in which exactly two elements appear only once and all ...

  4. LeetCode Single Number III (xor)

    题意: 给一个数组,其中仅有两个元素是出现1次的,且其他元素均出现2次.求这两个特殊的元素? 思路: 跟查找单个特殊的那道题是差不多的,只是这次出现了两个特殊的.将数组扫一遍求全部元素的异或和 x,结 ...

  5. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  6. leetcode 136 Single Number, 260 Single Number III

    leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...

  7. LeetCode 260. Single Number III(只出现一次的数字 III)

    LeetCode 260. Single Number III(只出现一次的数字 III)

  8. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

  9. [LeetCode] Single Number II 单独的数字之二

    Given an array of integers, every element appears three times except for one. Find that single one. ...

随机推荐

  1. android ScrollView滚动距离和判断滚动停止状态

    今天很高兴,自己解决了判断ScrollView滚动停止的监听,现在分享给大家. 因为ScrollView没有像listView中的setOnScrollListener()监听,当然也就没有SCROL ...

  2. 提升 web 应用程序的性能(二)

    最佳实践 本章将略述能帮助您提升 web 应用程序性能的最佳实践. 减少 HTTP 请求数 每个 HTTP 请求都有开销,包括查找 DNS.创建连接及等待响应,因此削减不必要的请求数可减少不必要的开销 ...

  3. OpenCV 2.4.10 Linux Qt Conifguration

    Download CMake 2.8.12 Extract the file, and run "./bootstrap", then "make", then ...

  4. Marching Cube

    GPU-Marching-Cubes An Implementation of the Marching Cubes[1] Algorithm Marching Cubes Matlab The St ...

  5. shenyi 语录

    [讲师]沈逸(65480539) 2016-06-08 14:58:42   会centos 转redhat是分分钟的事 [讲师]沈逸(65480539) 2016-06-08 14:58:54 查看 ...

  6. Reset RequiredFieldValidator 重置 验证控件

    <td style="width:100px;">姓名<span class="must_star">*</span>< ...

  7. nodejs:grunt使用合并压缩的基本使用

    一.模块化历史 1,nodejs出现:主要解决后端js规范 2,commonjs:这个组织出来一些服务器规范 3,后端规范commonjs应用升级到前端commonjs2:cmd规范(seajs)和完 ...

  8. CSS系列:表达式(Expression)`淘汰`

    概述 CSS表达式是动态设置CSS属性的强大(但危险)方法.Internet Explorer从第5个版本开始支持CSS表达式. 兼容性 expression方法在其它浏览器中不起作用,因此在跨浏览器 ...

  9. Gitolite配置管理和GIT基本操作

    简述公司版gitolite的项目配置与管理 1. 基于秘钥对的管理 1.1 客户端(需要访问代码库的机器)生成秘钥对,采用RSA加密ssh-keygen -t rsa -f path_to_store ...

  10. 2016.09.14,英语,《Using English at Work》全书笔记

    半个月时间,听完了ESLPod出品的<Using English at Work>,笔记和自己听的时候的备注列在下面.准备把每个语音里的快速阅读部分截取出来,放在手机里反复听. 下一阶段把 ...