题意:

  给一个数组,其中仅有两个元素是出现1次的,且其他元素均出现2次。求这两个特殊的元素?

思路:

  跟查找单个特殊的那道题是差不多的,只是这次出现了两个特殊的。将数组扫一遍求全部元素的异或和 x,结果也就是这两个特殊的元素的异或和了。现在必须找到两个当中的一个,才能恢复出另外一个。注意到x的二进制位中必定有1,那么只需要找到其中任意一个1对应的整数,再将数组中所有该位为1的计算他们的异或和a,a就是结果了,而另一个数b=x^a。

 vector<int> singleNumber(vector<int>& nums) {
int x=;
for(int i=; i<nums.size(); i++) x^=nums[i];
int lowbit=x-(x&(x-)), a=;
for(int i=; i<nums.size(); i++)
if( lowbit & nums[i] ) a^=nums[i];
return vector<int>{a,x^a};
}

AC代码

LeetCode Single Number III (xor)的更多相关文章

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

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

  2. LeetCode——Single Number II(找出数组中只出现一次的数2)

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

  3. LeetCode Single Number III

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

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

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

  5. LeetCode Valid Number 有效数字(有限自动机)

    题意:判断一个字符串是否是一个合法的数字,包括正负浮点数和整形. 思路:有限自动机可以做,画个图再写程序就可以解决啦,只是实现起来代码的长短而已. 下面取巧来解决,分情况讨论: (1)整数 (2)浮点 ...

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

  7. Single Number II(LintCode)

    Single Number II Given 3*n + 1 numbers, every numbers occurs triple times except one, find it. Examp ...

  8. [LeetCode] 137. Single Number II (位操作)

    传送门 Description Given an array of integers, every element appears three times except for one, which ...

  9. Leetcode Single Number II (面试题推荐)

    还记得<剑指offer>和<编程之美>等书上多次出现的找一个数组中仅仅出现一次的数那个题吗? leetcode也有这道题 链接here  相信大家都知道用异或在O(n)的时间复 ...

随机推荐

  1. Cactus详细讲解

    Cactus建议一年以上有经验的人玩,刚入门MVC,不了解下列组件请先自行学习,切勿好高骛远. Cactus的组成(基于.net4.5.2):Dapper+MVC4+autofac 前端css框架pu ...

  2. DMA缓冲区乒乓操作的处理

    http://www.51hei.com/bbs/dpj-141761-1.html https://blog.csdn.net/sunnydreamrain/article/details/8288 ...

  3. 使用配置类而不使用XML文件(代替bean.xml)对spring进行配置

    以下类是一个配置类,它的作用和bean.xml是一样的注解: @Configuration 作用: 用于指定当前类是一个spring配置类,当创建容器时会从该类上加载注解. 获取容器时需要使用Anno ...

  4. [Xcode 实际操作]四、常用控件-(8)UITextField控件的使用

    目录:[Swift]Xcode实际操作 本文将演示文本输入框控件的基本用法. 文本输入框主要用来接收和显示用户输入的内容. 在项目导航区,打开视图控制器的代码文件[ViewController.swi ...

  5. Windows日志为什么要把它转成Syslog呢?

    有的朋友会问,好好的Windows日志为什么要把它转成Syslog呢?呵呵,当Windows服务器比较少的时候,我们是不需要这样做的.但试想如果你管理着成千上百台的Windows机器,你会一台一台的登 ...

  6. JDK动态代理和cglib代理

    写一个简单的测试用例,Pig实现了Shout接口 public class MyInvocation implements InvocationHandler { Object k; public M ...

  7. Nacos深入浅出(九)

    然而Nacos的发布操作并不是上面我们想的那样通过代理去实现,通过下面的代码我们分析下: public class NacosConfigurationPropertiesBindingPostPro ...

  8. 洛谷P4114 Qtree1

    题目描述 给定一棵\(n\)个节点的树,有两个操作: \(CHANGE\) \(i\) \(t_i\) 把第\(i\)条边的边权变成\(t_i\) \(QUERY\) \(a\) \(b\) 输出从\ ...

  9. Vue里的nextTick方法

    官方解释: 在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的 DOM. 自己总结: `Vue.nextTick(callback)`,当数据发生变化,更新后执 ...

  10. CentOS 7 iptables 开放8080端口

    # 安装iptables-services [root@localhost bin]# yum install iptables-services [root@localhost bin]# /bin ...