1. 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?

在只有一个出现一次,其余数字出现两次的数组里面找出现一次的数字通过位异或即可实现。如果能将本题中的两个数字区分开来,问题也就可以解决了。思路同样是异或

  • step1:生成mask,将所有的数字都进行异或,最后的结果就是两个出现了一次的数字(记为a, b)的异或结果,这两个数字至少有1位不相等,将异或结果的出现为1的位置作为mask,mask的二进制只有1位是1
  • step2:寻找a和b,对于数组中的数字x,如果x&mask是1,说明x可能是a(或b);如果x&mask是0,说明x可能是b(或a)
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int x_or = 0;
for(auto x: nums)x_or ^= x;
int mask = 1;
while((mask & x_or) == 0)mask <<= 1;
int ans1 = 0, ans2 = 0;
for(auto x : nums){
if((mask & x) != 0){
ans1 ^= x;
}else{
ans2 ^= x;
}
}
return {ans1, ans2};
}
};

【刷题-LeeetCode】260. Single Number III的更多相关文章

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

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

  2. leetcode 136 Single Number, 260 Single Number III

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

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

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

  4. [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 ...

  5. 【一天一道LeetCode】#260. Single Number III

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. [LeetCode#260]Single Number III

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

  7. 【leetcode】260. Single Number III

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

  8. 【leetcode刷题笔记】Single Number II

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

  9. 【leetcode刷题笔记】Single Number

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

随机推荐

  1. Mac 远程上传文件到 Linux

    打开Mac自带终端: 在最顶端选择Shell ->新建远程连接: 选择sftp连接,填好服务器地址: 连接成功后.上传文件,使用 put 命令: put 本地文件路径 远程主机路径

  2. 第二周Python笔记之 变量的三元运算

    如果变量a小于b,则d的值取a变量的值,否则取c变量的值

  3. HTTPS 握手过程理解

    转自https://www.jianshu.com/p/a3a25c6627ee https://blog.csdn.net/xingtian713/article/details/11953057 ...

  4. 除了背八股文,Java面试更该这样准备

    我可以这样说,哪怕你背了再多java八股文的答案,过面试也能靠运气,因为很多java面试的答案只限于技术理论说辞.但用我本文给出的方法去准备面试,能在不提升技术的前提下,大大提升你java面试的通过率 ...

  5. c/c++一些常用的内置宏

    关于 本文演示环境: win10 + VS2017 Note 市面上的编译器五花八门,但是通常都支持: __DATE__,__FILE__,__LINE__ 和 __TIME__ 这个4个宏 VS20 ...

  6. 【linux项目】lichee nano linux烧写

    目录 前言 参考: 安装交叉编译链 搭建 SPI FLASH 烧录环境 让芯片进入烧写模式 sunxi 烧写命令 u-boot 裁剪 拉取 u-boot 源码 配置 u-boot 检查 flash 驱 ...

  7. A. Lorenzo Von Matterhorn

    A. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...

  8. 机器人的舞蹈(hdu 2232)

    机器人的舞蹈 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. golang切片的一些自问自答

    你好,我是轩脉刃.这篇是关于go切片的一些问题和回答. go的切片基本上是代码中使用最多的一种数据结构了,使用这种数据结构有哪些要注意的点,这个是非常必要了解的东西.基本上,以前写的一篇博客 http ...

  10. <学习opencv>图像和大型阵列类型

    OPenCV /*=========================================================================*/ // 图像和大型阵列类型 /* ...