原题链接在这里: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. git 创建branch分支

    开发者user1 负责用getopt 进行命令解析的功能,因为这个功能用到getopt 函数,于是将这个分支命名为user1/getopt.(1)确保是在开发者user1的工作区中cd /home/j ...

  2. jQuery.Validate验证库详解

    一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassist ...

  3. NSNumber的使用

    1.NSNumber可以表示多种基本数据类型,如int.bool.char.float.double,以及他们加了修饰符long.unsigned的类型.     2.创建方法可以使用numberWi ...

  4. PHP一般情况下生成的缩略图都比较不理想

    PHP用GD库生成高质量的缩略图片,PHP一般情况下生成的缩略图都比较不理想.今天试用PHP,GD库来生成缩略图.虽然并不100%完美.可是也应该可以满足缩略图的要求了.<?php $FILEN ...

  5. SQL阻止保存要求重新创建表的更改 在哪里设置

    ef生成的数据表,有数据,设计的时候,想把某个字段改成可为null. 报 “阻止保存要求重新创建表”错误 百度一下: 修改数据库表结构时提示[不允许保存更改.您所做的更改要求删除并重新创建以下表.您对 ...

  6. Frenetic HelloSDNWorld

    Follow Frenetic-Github HelloSDNWorld 实验环境: Frenetic虚拟机: 实验步骤: 1.Start up a terminal window - – two a ...

  7. shell 中的数学计算

    1.1.第一种——expr格式:expr 操作数 1 操作符 操作数 2举例:      1 expr 1 + 2 TMP=$(expr 1 + 2)      2 expr 1 + 4 / 3 TM ...

  8. web_custom_request函数详解

    在LR中当使用HTML录制方式时,录制的脚本中主要由函数web_link().web_submit_form().web_url().web_submit_data()组成,当使用HTTP录制方式时, ...

  9. SQL搜索下划线,like中不能匹配下划线的问题

    最近在检测天气预报15天查询网 站(http://tqybw.net)时的URL时,发现页面中有很些404页,分析发现,是请求地址的能参数中多了下划线“_”,而rewrite规 则中并没有配这样的规则 ...

  10. nginx 配置多个二级域名

    server { server_name domain.com www.domain.com *.domain.com ; set $subdomain ''; if ($host ~* (\b(?! ...