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:
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?

方法一:

首先计算nums数组中所有数字的异或,记为xor
令lowbit = xor & -xor,lowbit的含义为xor从低位向高位,第一个非0位所对应的数字
例如假设xor = 6(二进制:0110),则-xor为(二进制:1010,-6的补码)
则lowbit = 2(二进制:0010)

 class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int size=nums.size();
if(size==||nums.empty())
return vector<int>();
int diff=;
for(auto &ele:nums)
diff^=ele;
vector<int> res(,);
diff&=-diff;//从低位向高位,第一个非0位所对应的数字
for(auto &ele:nums)
if(diff&ele)
res[]^=ele;
else
res[]^=ele;
return res;
}
};

方法二:

 class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int size=nums.size();
if(size==||nums.empty())
return vector<int>();
int sum=;
for(auto &ele:nums)
sum^=ele;
vector<int> res(,);
//从低位向高位,第一个非0位所对应的数字
int n=;
while((sum&n)==)
n=n<<;
for(auto &ele:nums)
if(n&ele)
res[]^=ele;
else
res[]^=ele;
return res;
}
};

LeetCode 260 Single Number III 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数的更多相关文章

  1. LeetCode 137 Single Number II 数组中除了一个数外,其他的数都出现了三次,找出这个只出现一次的数

    Given an array of integers, every element appears three times except for one, which appears exactly ...

  2. 260 Single Number III 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数

    给定一个整数数组 nums,其中恰好有两个元素只出现一次,其他所有元素均出现两次. 找出只出现一次的那两个元素.示例:给定 nums = [1, 2, 1, 3, 2, 5], 返回 [3, 5].注 ...

  3. 137 Single Number II 数组中除了一个数外,其他的数都出现了三次,找出这个只出现一次的数

    给定一个整型数组,除了一个元素只出现一次外,其余每个元素都出现了三次.求出那个只出现一次的数.注意:你的算法应该具有线性的时间复杂度.你能否不使用额外的内存来实现?详见:https://leetcod ...

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

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

  5. [LeetCode] 260. Single Number III 单独数 III

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

  6. [LeetCode] 260. Single Number III(位操作)

    传送门 Description Given an array of numbers nums, in which exactly two elements appear only once and a ...

  7. Java [Leetcode 260]Single Number III

    题目描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...

  8. Leetcode 260 Single Number III 亦或

    在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次) 同样用亦或来解决(参考编程之美的1.5) 先去取出总亦或值 然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1] a&am ...

  9. [LeetCode#260]Single Number III

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

随机推荐

  1. Guice总结

    Guice总结 Jar包:guice-4.1.0.jar 辅包: guava-15.0.jar aopalliance-.jar javaee-api-6.0-RC2.jar Guice的IoC 两种 ...

  2. Starting MySQL....The server quit without updating PID file错误解决办法

    出现错误:Starting MySQL....The server quit without updating PID file 检查错误文件: /var/lib/mysql/xxxx.err,根据其 ...

  3. Erlang generic standard behaviours -- summary

    gen_server 相关的片段分析得也差不多了, 这篇作为一个简要的总结.这一系列相关的分析暂且告一段落(之后如有必要,还会回来的 ^^ ),下一个系列主要是以pool 相关, 包括但不仅限于开源项 ...

  4. DSP/BIOS程序启动顺序

    基于TI的DSP芯片的应用程序分为两种:一般应用程序:DSP/BIOS应用程序. 为简化编程,TI提供了一套C的编程接口,它以API和宏的形式封装了TI的所有硬件模块,这套接口统称DSP/BIOS.D ...

  5. JavaScript-Tool:my97datepicker

    ylbtech-JavaScript-Tool:my97datepicker 1.返回顶部 1. 2.下载 https://files.cnblogs.com/files/storebook/java ...

  6. Dialog 自定义使用1

    一: 布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...

  7. Impala介绍

    Impala介绍 Impala支持的文件格式 Impala可以对Hadoop中大多数格式的文件进行查询.它能通过create table和insert的方式将一部分格式的数据加载到table中,但值得 ...

  8. ubuntu下hive-0.8.1配置

    1.下载hive包wget http://labs.mop.com/apache-mirror/hive/stable/hive-0.8.1.tar.gz,并用tar -xzvf 将其解压到要安装的目 ...

  9. tensorflow placeholder

    placeholder 是 Tensorflow 中的占位符,暂时储存变量. Tensorflow 如果想要从外部传入data, 那就需要用到 tf.placeholder(), 然后以这种形式传输数 ...

  10. 线程池和Thread

    1.线程池 创建线程需要时间.如果有不同的短任务要完成,就可以事先创建许多线程,在应完成这些任务时发出请求.这个线程数最好在需要更多线程时增加,在需要释放资源时减少.不需要自己创建这样一个列表.该列表 ...