leetcode 260. Single Number III 神奇的位操作 + 找到两个只出现一次的数

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:
The order of the result is not important. So in the above example, [5, 3] is also correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

这道题很简单,最直接的方法就是使用HashMap计数,遍历一次即可,但是要是使用位操作之类的做法,想不出来。

建议和这一道题leetcode 136. Single Number 位操作leetcode 137. Single Number II 一起学习。

代码如下:


/* 
 * https://segmentfault.com/a/1190000004886431
 * 
 *  通过遍历整个数组并求整个数组所有数字之间的 XOR,
 *  根据 XOR 的特性可以得到最终的结果为 AXORB = A XOR B;
 *  
 *  通过某种特定的方式,我们可以通过 AXORB 得到在数字 A 和数字 B 
 *  的二进制下某一位不相同的位;因为A 和 B 是不相同的,所以他们的二进制数字有
 *  且至少有一位是不相同的。我们将这一位设置为 1,并将所有的其他位设置为 0,
 *  我们假设我们得到的这个数字为 bitFlag;
 *  
 *  那么现在,我们很容易知道,数字 A 和 数字 B 中必然有一个数字与上 bitFlag 为 0;
 *  因为bitFlag 标志了数字 A 和数字 B 中的某一位不同,那么在数字 A 和 B 中的这一位
 *  必然是一个为 0,另一个为 1;而我们在 bitFlag 中将其他位都设置为 0,那么该位为 0 
 *  的数字与上 bitFlag 就等于 0,而该位为 1 的数字与上 bitFlag 就等于 bitFlag
 *  
 *  现在问题就简单了,我们只需要在循环一次数组,将与上 bitFlag 为 0 的数字进行 XOR 运算,
 *  与上 bitFlag 不为 0 的数组进行独立的 XOR 运算。那么最后我们得到的这两个数字就是 A 和 B。
 * 
 * */
class Solution
{
    public int[] singleNumber(int[] nums) 
    {
        int[] res=new int[2];
        int AXORB=0;
        for(int i=0;i<nums.length;i++)
            AXORB ^= nums[i];

        /*
         * 这一行代码的作用是:找到数字 A 和数字 B 中不相同的一位,并将该位设置为 1,其他位设置为 0;
         * 
         * 根据 XOR 的定义,我们知道,在 AXORB 中,为 1 的位即 A 和 B 不相同的位,AXORB 
         * 中为 0 的位即 A 和 B 中相同的位
         * 所以,要找到 A 和 B 中不相同的位,只需要找到在 AXORB 中从右往左第一个为 1 的位,
         * 保留该位并将其他位置为 0 即可。
         * 
         * //其实这一行与下面的代码等价,但是论逼格就差远了(手动斜眼)
            public static int f(int num){
                int times = 0;
                while(num > 0){
                    if(num % 2 == 1)
                        break;
                    times++;
                    num = num >> 1;  }
                return 1 << times;  }
         * */
        int bitflag=AXORB & (~(AXORB-1));

        for(int i=0;i<nums.length;i++)
        {
            if((nums[i]&bitflag)==0)
                res[0]^=nums[i];
            else 
                res[1]^=nums[i];
        }
        return res;
    }
}

下面是C++的做法,就是一个简单的遍历,使用HashMap遍历即可

代码如下:

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>

using namespace std;

class Solution 
{
public:
    vector<int> singleNumber(vector<int>& nums) 
    {
        vector<int> res;
        map<int, int> mmp;
        for (int a : nums)
        {
            if (mmp.find(a) == mmp.end())
                mmp[a] = 1;
            else
                mmp[a] += 1;
        }

        map<int, int>::iterator i = mmp.begin();
        for (; i != mmp.end(); i++)
        {
            if (i->second == 1)
                res.push_back(i->first);
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值