Leetcode—421.数组中两个数的最大异或值【中等】明天写一下字典树做法!!!

2023每日刷题(十九)

Leetcode---421.数组中两个数的最大异或值

算法思想

参考自灵茶山艾府

实现代码

cpp 复制代码
class Solution {
public:
    int findMaximumXOR(vector<int>& nums) {
        int maxValue = *max_element(nums.begin(), nums.end());
        int highIdx = maxValue ? 31 - __builtin_clz(maxValue) : -1;
        int ans = 0;
        int mask = 0;
        unordered_set<int> pre;
        for(int i = highIdx; i >= 0; i--) {
            mask |= 1 << i;
            int new_ans = ans | (1 << i);
            pre.clear();
            for(auto x: nums) {
                x &= mask;
                if(pre.contains(x ^ new_ans)) {
                    ans = new_ans;
                    break;
                }
                pre.insert(x);
            }
        }
        return ans;
    }
};

运行结果

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关推荐
文丑颜不良啊18 小时前
LeetCode2981~3000题目解析
java·leetcode
罗西的思考19 小时前
【轻量化复刻大模型 / KIMI K3】Multi-Token Prediction(MTP / EAGLE-3)(2)
人工智能·算法·机器学习
罗西的思考19 小时前
【轻量化复刻大模型 / KIMI K3】Multi-Token Prediction(MTP / EAGLE-3)(1)
人工智能·算法·机器学习
武子康21 小时前
H3-World 接入 Game Agent:谁来判断门真的打开了?
人工智能·算法·机器学习
南风84111 天前
C++ 四种强制类型转换详解
c++
微笑星河q1 天前
C++STL【vector】常用接口函数使用和迭代器失效问题
开发语言·c++
文丑颜不良啊1 天前
LeetCode3001~3020题目解析
java·leetcode
HaiXCoder1 天前
C++ 表达式怎么读:从一行代码到类型、值类别和生命周期
c++
武子康1 天前
H3-World 扩展跳跃与开门:新动作怎样进入训练数据?
大数据·人工智能·算法
卡尔曼的BD SLAMer1 天前
北斗导航 |GNSS ION论文集汇总1997到2022(链接在文末)
算法·北斗导航