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?

思路:对所有数进行一次异或运算。运算结果为所求的两个数的异或结果。

因为两个数是不同的,因此结果必不为0。设该结果为res,其二进制形式必有几位是1。而根据异或运算规则,所求的两个数在这些位上一定有一个是1,一个是0。我们从这些位中随便取1位,然后将所有的数根据在这一位上是0还是1分成两组。则所求的两个数必然分别在这两个组内,此时分别再进行一次异或运算,就得到了两个数。

代码中 res随机取二进制为1的一位 用的是 res &= -res;

可以这样做的原因是,一个数的负为它的反码加1, 如数00100100,反码是11011011,将其加一后变为11011100,可以看到,原数最右的一个1现在仍然是1,其他位与运算完后是0。这样两数与运算后结果为00000100,即只有最右的一个1。

此外,for (:)的写法也是第一次见。

 class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int res = ;
for (int num : nums)
res ^= num;
res &= -res;
vector<int> ans = {, };
for (int num : nums)
{
if (res & num)
ans[] ^= num;
else ans[] ^= num;
}
return ans;
}
};

Single Number III - LeetCode的更多相关文章

  1. Single Number III leetcode java

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

  2. Single Number III——LeetCode

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

  3. leetcode 136 Single Number, 260 Single Number III

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

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

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

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

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

  6. Single Number III(LintCode)

    Single Number III Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example G ...

  7. LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

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

  8. 【刷题-LeeetCode】260. Single Number III

    Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...

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

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

随机推荐

  1. Android开发——用户在屏幕上的手势识别

    个定点决定.四个属性分别为left(1),top(2),right(3),bottom(4). 数字为图上标出的距离.显然这四个属性是相对于父容器来定的,均可以通过get()方法获取. 因此很容易得出 ...

  2. 基类View

    尽管类视图看上去类的种类繁多,但每个类都是各司其职的,且从类的命名就可以很容易地看出这个类的功能.大致可分为如下三个大的功能块,分别由三个类提供对应的方法: 处理 HTTP 请求.根据 HTTP 请求 ...

  3. 【POI 2010】反对称 Antisymmetry

    题目: 对于一个 $0/1$ 字符串,如果将这个字符串 $0$ 和 $1$ 取反后,再将整个串反过来和原串一样,就称作「反对称」字符串.比如 $00001111$ 和 $010101$ 就是反对称的, ...

  4. alert(1) to win部分解题

    XSS在线习题分析 (https://alf.nu/alert1) 1. Warmup function escape(s) { return '<script>console.log(& ...

  5. Python+Selenium中级篇之-二次封装Selenium中几个方法

    本文来介绍,如何把常用的几个webdriver的方法封装到自己写的一个类中去,这个封装过程叫二次封装Selenium方法.我们把打开站点,浏览器前进和后退,关闭和退出浏览器这这个方法封装到一个新写的类 ...

  6. Halcon11 Linux 下载

    Halcon11 Linux下载地址:http://www.211xun.com/download_page_3.html HALCON 11 是一套机器视觉图像处理库,由一千多个算子以及底层的数据管 ...

  7. maven学习(九)——maven中的坐标、依赖以及仓库

    一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1.2.Maven坐标主要组成 groupId:组织标识(包名) artifactId:项目名称 ver ...

  8. EasyUi DataGrid 获取选中行的checkbox

    EasyUi DataGrid 获取选中行的checkbox var checked = $(":checkbox[name=ID]:checked"); console.log( ...

  9. C#中找不到MouseWheel事件的解决办法

    在.....Designer.cs中加入 this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this. ...

  10. hdu5834 Magic boy Bi Luo with his excited tree 【树形dp】

    题目链接 hdu5834 题解 思路很粗犷,实现很难受 设\(f[i][0|1]\)表示向子树走回来或不回来的最大收益 设\(g[i][0|1]\)表示向父亲走走回来或不回来的最大收益 再设\(h[i ...