Finding Two Numbers That Appear Once in Array of Duplicated Numbers

中文题目:一个数组中所有数字都是成双成对出现的,只有两个数只出现一次,找出来哪两个数

先来做一题简单的:假设只有一个数,而不是两个数。

这里用到了神奇的异或。我们知道异或是具有结合律的。所以把整串数字异或下,最后就是0与所求数字的异或,也就是所求数字。

//无视我的库文件~
#include<iostream>
#include<string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
int main()
{
    int a[] = { 1, 2, 33, 55, 44, 66, 9, 2, 1, 55, 44, 66, 33 };
    int r=0;
    for (auto i: a)
    {
        r ^= i;
    }
    cout << r;
}

下面如果是两个呢?

先全部异或一次,得到的要求的两个数的异或,取里面不同的部分,即某一位为1的那个作为区分分组。

x&-x保留最低位的1。(-x为取补码)或者x&~(n-1)效果是一样的。

代码如下:

//无视我的库文件
#include<iostream>
#include<string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
void find(vector<int> a)
{
    int r = 0, t = 0;
    for (auto i: a)
    {
        r ^= i;
    }
    for (auto i : a)
    {
        if (i&r&-r)
        {
            t ^= i;
        }
    }
    cout << t << " " << (t^r) << endl;
}
int main()
{
    vector<int> a{ 1, 2, 33, 55, 44, 66, 9, 2, 1, 55, 44, 66, 33, 72};
    find(a);
    system("pause");
}

还有一种求这上面的t的方法,如下:

int t= 1;
            while ((r& t) == 0)
                t<<= 1;

更多关于异或的参考这里大神的文章

更多关于Markdown相关参考这里

posted on 2016-03-29 19:38 sayhitrue 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/sayhitrue/p/5333616.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值