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;
    }
};

运行结果

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

相关推荐
随流寻影1 小时前
利用 C++ 实现快速排序及三种优化
开发语言·c++
Nazarick2 小时前
力扣LeetCode热题75题(1)
java·算法·leetcode·力扣
西西弗Sisyphus2 小时前
C++ 最小 Agentic Loop 实现 - 回调函数系列 - 指针与内存地址(2)
开发语言·c++
16月6日-晴2 小时前
斐波那契数列题解
算法·leetcode
郝学胜-神的一滴3 小时前
Effective Python 条款47:吃透惰性属性
大数据·开发语言·数据库·人工智能·python·算法·oracle
西西弗Sisyphus3 小时前
C++ 最小 Agentic Loop 实现 - 回调函数系列 - typedef 封装(4)
c++
笑鸿的学习笔记3 小时前
C++笔记之信号量和引用计数的概念是否等价?
java·c++·笔记
Keven_114 小时前
算法札记:有限差分
算法
忙里偷闲学python4 小时前
GK2A 林火蔓延预报算法说明
算法
我是伪码农4 小时前
11.21程序设计
数据结构·算法