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. Java并发——synchronized和ReentrantLock的联系与区别

    0 前言 本文通过使用synchronized以及Lock分别完成"生产消费场景",再引出两种锁机制的关系和区别,以及一些关于锁的知识点. 本文原创,转载请注明出处:http:// ...

  2. ajax提交表单,支持文件上传

    当我们提交表单但是又不想要刷新页面的时候就可以考虑使用ajax来实现提交功能,但是这有个局限就是当有文件上传的时候是行不通的,下面借助于jquery.form可以很方便满足我们的需求.   1.表单写 ...

  3. 【IPv6】ISATAP隧道技术详解

    一.基本概念       ISATAP(Intra-SiteAutomatic Tunnel Addressing Protocol)    ISATAP是一种非常容易部署和使用的IPv6过渡机制.在 ...

  4. 使用hibernate建立mysql连接以及生成映射类和配置文件*.cfg.xml

    建立数据库连接 找到window—open perspective—myeclipse database explore空白出右键new注意 driver template 和driver class ...

  5. [python][django学习篇][9]设计正在博客视图(3)

    需求: 真正的首页视图函数,当用户访问我们的博客首页时,他将看到我们发表的博客文章列表,就像 演示项目 里展示的这样.t https://docs.djangoproject.com/en/1.10/ ...

  6. SQLSERVER 数据库基础操作

    1.修改表中字段的长度,类型为varchar,从30改到50    语句执行(注:当前为30):    alter table 表名 alter column 列名 varchar(50)  2.增加 ...

  7. asp.net允许跨域访问

    C# ASP.NET MVC 配置允许跨域访问 在web.config文件中的 system.webServer 节点下 增加如下配置 <httpProtocol> <customH ...

  8. 基于Jquery的商城商品图片的放大镜效果(非组件)

    在开发商城的时候,往往会用到图片的放大功能,这里把自己在近期项目中使用的放大镜特效做一下总结(非插件). 放大镜效果 常用的js组件jquery.imagezoom,jquery.jqzoom,jqu ...

  9. Educational Codeforces Round 22 E. Army Creation(分块好题)

    E. Army Creation time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  10. include和require的区别误区

    面试时总会被问到include和require的区别,回答的时候一般也是有以下几种区别: 1.include引入文件的时候,如果碰到错误,会给出警告,并继续运行下边的代码. require引入文件的时 ...