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?

题目很简单,但是通过这个题目让我见识了LeetCode众多大牛的存在。

代码中,第一次了使用accumulate函数,然后还要bit_xor<int>()之类的C++11的function object。

此处如果diff = INT_MIN, 那么 -diff 恰好会溢出到 INT_MIN. 之前并没有注意过这个问题。

  1. class Solution {
  2. public:
  3. vector<int> singleNumber(vector<int>& nums) {
  4. int diff = accumulate(begin(nums), end(nums), , bit_xor<int>());
  5. diff &= -diff;
  6. vector<int> result = {, };
  7. for (auto num: nums) {
  8. result[!(diff & num)] ^= num;
  9. }
  10. return result;
  11. }
  12. };

LeeCode-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. Single Number III(LintCode)

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

  5. 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 ...

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

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

  7. [LeetCode] 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 III

    原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...

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

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

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

随机推荐

  1. Spring Cloud Feign设计原理

    什么是Feign? Feign 的英文表意为“假装,伪装,变形”, 是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求,而不用像Java中通过封装HTTP请求报文的方式直 ...

  2. uoj140 【UER #4】被粉碎的数字

    题目 看起来就像是数位\(\rm dp\) 不妨从竖式乘法的角度来考虑这个问题 为了方便处理进位,我们得从低位向高位填数 设\(dp[i][0/1][j][p][t]\)表示填到了第\(i\)位,卡不 ...

  3. docker使用gitlab持续集成(1)

    修改ssh连接端口vi /etc/ssh/sshd_config 写docker-compose.yml文件配置gitlab version: '3' services: gitlab: image: ...

  4. mysql连接数问题备份

    一. max_connections 这是是查询数据库当前设置的最大连接数 mysql> show variables like '%max_connections%';+----------- ...

  5. UC浏览器禁止图片阅读模式处理方法

    本文转载自:https://www.cnblogs.com/MY0101/p/9969818.html UC浏览器点击图片会出现图片阅读模式. 如何处理? <img style=" w ...

  6. Jquery中attr与prop的区别详解

    prop()函数的结果: 1.如果有相应的属性,返回指定属性值. 2.如果没有相应的属性,返回值是空字符串. attr()函数的结果: 1.如果有相应的属性,返回指定属性值. 2.如果没有相应的属性, ...

  7. Windows 隐藏控制台

    #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"& ...

  8. CSS3——动画

    动画是CSS3中具有颠覆性的特征之一,可通过设置多个节点来精确控制一个或一组动画,常用来实现复杂的动画效果. 语法格式:  animation:动画名称 动画时间 运动曲线 何时开始 播放次数 是否反 ...

  9. thinkphp DEFINED标签

    DEFINED标签用于判断某个常量是否有定义,用法如下: 大理石平台检验标准 <defined name="NAME"> NAME常量已经定义 </defined ...

  10. 如何查看redis占用内存大小

    redis缓存固然高效,可是它会占用我们系统中宝贵的内存资源,特别是当我们的项目运行了一段时间后,我们需要看一下redis占用了多少内存,那么可以用“info”命令查看. 执行info命令后,找到Me ...