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

运行结果

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

相关推荐
leaves falling几秒前
c语言分数求和
算法
Das11 分钟前
【机器学习】01_模型选择与评估
人工智能·算法·机器学习
星轨初途32 分钟前
郑州轻工业大学2025天梯赛解题
c++·经验分享·笔记·算法·链表·剪枝
不忘不弃1 小时前
从字符串中提取数字
数据结构·算法
点云SLAM1 小时前
C++ 引用折叠(Reference Collapsing)和示例讲解说明
数据结构·c++·标准算法·完美转发·代码性能优化·c++ 引用折叠·typedef / using
囊中之锥.1 小时前
《机器学习SVM从零到精通:图解最优超平面与软间隔实战》
算法·机器学习·支持向量机
chenyuhao20242 小时前
Linux网络编程:HTTP协议
linux·服务器·网络·c++·后端·http·https
Minecraft红客2 小时前
ai_dialogue_framework项目1.0(纯原创)
c++·测试工具·电脑
必胜刻2 小时前
复原 IP 地址(回溯算法)
tcp/ip·算法·深度优先
YGGP2 小时前
【Golang】LeetCode 5. 最长回文子串
算法·leetcode