刷题总结#4

#421
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231.

Find the maximum result of ai XOR aj, where 0 ≤ i, j < n.
Input: [3, 10, 5, 25, 2, 8]

Output: 28

Explanation: The maximum result is 5 ^ 25 = 28.
挺有意思的一个题。异或的题一般都是从每一位整体的情况入手。
主要得明白 A^B=C , A^C=B;
我们从高位入手,用10000……, 11000……,这样的数&去获取高位。

 int ans=0;
        int bar=0;
        for(int i=30;i>=0;i--)
        {
            bar|=1 < < i;
            unordered_set<int>pq;
            for(int j=0;j < nums.size();j++)
            {
                pq.insert(nums[j]&bar);
            }
            //获取高位,然后放到pq的set里面
            int tmp=ans|1 < < i;//假设这一位可以加到ans里面
            for(auto &it:pq)
            {
                if(pq.find(it^tmp)!=pq.end()) 通过A^ans找B来判断是否ans成立
                {
                    ans=tmp;
                    break;
                }
            }
        }
        return ans;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值