在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次)

同样用亦或来解决(参考编程之美的1.5)

先去取出总亦或值

然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1]

a&(-a)就是计算出这个数,可以参考树状数组。

最后就是注意(nums[i] & a) == 0要加()。注意符号运算优先级

 class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int a = ;
for(int i = ; i<nums.size(); ++i){
a ^= nums[i];
}
vector<int> ans(,);
a &= (-a);
for(int i = ; i<nums.size(); ++i){
ans[ (nums[i] & a) == ] ^= nums[i];
}
return ans;
}
};

Leetcode 260 Single Number III 亦或的更多相关文章

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

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

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

  3. [LeetCode#260]Single Number III

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

  4. Java [Leetcode 260]Single Number III

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

  5. LeetCode 260 Single Number III 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数

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

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

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

  7. leetcode 136 Single Number, 260 Single Number III

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

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

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

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

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

随机推荐

  1. 50个SQL语句

    一个项目用到的50个SQL语句 转载 说明:下面五十个语句都依照測试数据进行过測试.最好每次仅仅单独执行一个语句. 问题及描写叙述: --1.学生表 Student(Sid,Sname,Sage,Ss ...

  2. 原生js螺旋运动

    window.onload=function(){ var oSpiral=document.getElementById('spiral'); var oUl=oSpiral.getElements ...

  3. leetcode笔记:Remove Duplicates from Sorted Array II

    一.题目描写叙述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是相似的.仅仅只是这里同意出现反复的数字而已,能够採用二分搜索的变种算法.仅仅只是增 ...

  4. 数据类型总结——Boolean类型(布尔类型)

    相关文章 简书原文:https://www.jianshu.com/p/e5c75d4be636 数据类型总结——概述:https://www.cnblogs.com/shcrk/p/9266015. ...

  5. angular动画知识点以及代码样例

    原文地址 https://www.jianshu.com/p/4400174072e2 大纲 1.angular动画的相关概念 2.angular动画的绑定方式 3.angular动画代码实例 1.a ...

  6. Matlab-------regexp正则表达式

    转自原文 Matlab-------regexp正则表达式 句点符号 '.' ——匹配任意一个(只有一个)字符(包括空格). 例如:t.n,它匹配tan. ten.tin和ton,还匹配t#n.tpn ...

  7. 【codeforces 742C】Arpa's loud Owf and Mehrdad's evil plan

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. UVA 1564 - Widget Factory(高斯消元)

    UVA 1564 - Widget Factory 题目链接 题意:n种零件, 给定m个制作时间.每段时间制作k个零件,每种零件有一个制作时间,每段时间用Mon到Sun表示,求每一个零件的制作时间.还 ...

  9. 封装springmvc处理ajax请求结果

    原文链接:http://blog.csdn.net/qq_37936542/article/details/79064818 需求描述:ajax向后台发起请求,springmvc在处理完请求后返回的结 ...

  10. Python将被加入高考科目?你怎么看?

    今天看到这样的一则新闻:不禁感叹,人工智能这股风来的太快,已经掀起全民学习Python的浪潮. 2017年中观察:看上去这个大纲内容基本是这样了,但是实行年份可能要往后推了,不在2017年执行了(据说 ...