421. Maximum XOR of Two Numbers in an Array[Medium](Leetcode每日一题-2021.05.15)--抄答案

本文主要是介绍421. Maximum XOR of Two Numbers in an Array[Medium](Leetcode每日一题-2021.05.15)--抄答案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Problem

Given an integer array nums, return the maximum result of nums[i] XOR nums[j], where 0 ≤ i ≤ j < n.

Constraints:

  • 1 <= nums.length <= 2 * 104
  • 0 <= nums[i] <= 231 - 1

Example1

Input: nums = [3,10,5,25,2,8]
Output: 28
Explanation: The maximum result is 5 XOR 25 = 28.

Example2

Input: nums = [0]
Output: 0

Example3

Input: nums = [2,4]
Output: 6

Example4

Input: nums = [8,10,2]
Output: 10

Example5

Input: nums = [14,70,53,83,49,91,36,80,92,51,66,70]
Output: 127

Solution

struct Trie {// 左子树指向表示 0 的子节点Trie* left = nullptr;// 右子树指向表示 1 的子节点Trie* right = nullptr;Trie() {}
};class Solution {
private:// 字典树的根节点Trie* root = new Trie();// 最高位的二进制位编号为 30static constexpr int HIGH_BIT = 30;public:void add(int num) {Trie* cur = root;for (int k = HIGH_BIT; k >= 0; --k) {int bit = (num >> k) & 1;if (bit == 0) {if (!cur->left) {cur->left = new Trie();}cur = cur->left;}else {if (!cur->right) {cur->right = new Trie();}cur = cur->right;}}}int check(int num) {Trie* cur = root;int x = 0;for (int k = HIGH_BIT; k >= 0; --k) {int bit = (num >> k) & 1;if (bit == 0) {// a_i 的第 k 个二进制位为 0,应当往表示 1 的子节点 right 走if (cur->right) {cur = cur->right;x = x * 2 + 1;}else {cur = cur->left;x = x * 2;}}else {// a_i 的第 k 个二进制位为 1,应当往表示 0 的子节点 left 走if (cur->left) {cur = cur->left;x = x * 2 + 1;}else {cur = cur->right;x = x * 2;}}}return x;}int findMaximumXOR(vector<int>& nums) {int n = nums.size();int x = 0;for (int i = 1; i < n; ++i) {// 将 nums[i-1] 放入字典树,此时 nums[0 .. i-1] 都在字典树中add(nums[i - 1]);// 将 nums[i] 看作 ai,找出最大的 x 更新答案x = max(x, check(nums[i]));}return x;}
};

这篇关于421. Maximum XOR of Two Numbers in an Array[Medium](Leetcode每日一题-2021.05.15)--抄答案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/570086

相关文章

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

poj 3104 二分答案

题意: n件湿度为num的衣服,每秒钟自己可以蒸发掉1个湿度。 然而如果使用了暖炉,每秒可以烧掉k个湿度,但不计算蒸发了。 现在问这么多的衣服,怎么烧事件最短。 解析: 二分答案咯。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <c

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN