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

运行结果

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

相关推荐
垆边人似月.11 小时前
城市连通性(200分 / 并查集)
数据结构·算法·图论
西西弗Sisyphus11 小时前
Qt6 实现 会动的界面像物理世界一样
c++·qt
UIU11411 小时前
头文件可以防什么错?运用头文件做声明
c++·学习·c#
Gust of wind11 小时前
串与KMP模式匹配:存储结构、基本操作、next数组手算
c语言·数据结构·后端·算法
Navigator_Z11 小时前
LeetCode //C - 1283. Find the Smallest Divisor Given a Threshold
c语言·算法·leetcode
by2099912 小时前
list:迭代器与资源管理实现的综合叙述(下)
数据结构·c++·笔记·list
啦啦啦啦啦zzzz12 小时前
一致性哈希
服务器·算法·哈希算法·c++20·分布式存储
我不会起名字32212 小时前
二叉树的遍历与二叉搜索树:把递归、迭代、还原树一次讲透
数据结构·leetcode·二叉树·二叉搜索树
MayZork12 小时前
Qt 开发集成CMake + vcpkg
开发语言·c++·qt
垆边人似月.12 小时前
嵌入式传感器寄存器数据解析
算法