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

运行结果

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

相关推荐
sheeta199842 分钟前
LeetCode 每日一题笔记 日期:2026.06.06 题目:2196. 根据描述创建二叉树
笔记·算法·leetcode
小欣加油1 小时前
leetcode994 腐烂的橘子
数据结构·c++·算法·leetcode·bfs
.千余2 小时前
【C++】手写双向链表:list容器模拟实现
开发语言·c++·笔记·学习·其他
QuZero2 小时前
Guava Cache Deep Dive
java·后端·算法·guava
随意起个昵称2 小时前
线性dp-LIS题目4(A Twisty Movement)
算法·动态规划
liulilittle2 小时前
过冲:拥塞控制的呼吸与盲行
linux·网络·c++·tcp/ip·计算机网络·tcp·通信
Felven2 小时前
B. Fair Numbers
数据结构·算法
人道领域2 小时前
【LeetCode刷题日记】93.复原IP地址
java·开发语言·算法·leetcode
jarreyer2 小时前
【算法记录1】模型训练问题
算法
Felven2 小时前
D. Friends and the Restaurant
算法