leetcode每日一题
[1738] 找出第 K 大的异或坐标值
[1744] 你能在你最喜欢的那天吃到你最喜欢的糖果吗?
[231] 2的幂
堆排复习
[461] 汉明距离
[1190] 反转每对括号间的子串
[1707] 与数组中元素的最大异或值
[810] 黑板异或游戏
[1035] 不相交的线
[692] 前K个高频单词
[872] 叶子相似的树
[1442] 形成两个异或相等数组的三元组数目
[993] 二叉树的堂兄弟节点
[421] 数组中两个数的最大异或值
[13] 罗马数字转整数
[12] 整数转罗马数字
[1269]. 停在原地的方案数
[1310] 子数组异或查询
[1734] 解码异或后的排列
本文档使用 MrDoc 发布
-
+
首页
[421] 数组中两个数的最大异或值
## [421] 数组中两个数的最大异或值 [力扣原题链接](https://leetcode-cn.com/problems/maximum-xor-of-two-numbers-in-an-array/) 给你一个整数数组 `nums` ,返回 `nums[i] XOR nums[j]` 的最大运算结果,其中 `0 ≤ i ≤ j < n` 。 进阶:你可以在 `O(n)` 的时间解决这个问题吗? **示例 1:** <pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><p><code><span class="pun"> 输入:</span><span class="pln">nums </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="lit">3</span><span class="pun">,</span><span class="lit">10</span><span class="pun">,</span><span class="lit">5</span><span class="pun">,</span><span class="lit">25</span><span class="pun">,</span><span class="lit">2</span><span class="pun">,</span><span class="lit">8</span><span class="pun">]</span></code></p></li><li class="L1" data-node-id="20210516220557-mbyftgx"><p><code><span class="pun"> 输出:</span><span class="lit">28</span></code></p></li><li class="L2"><p><code><span class="pun"> 解释:最大运算结果是 </span><span class="pln"> </span><span class="lit">5</span><span class="pln"> XOR </span><span class="lit">25</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="lit">28.</span></code></p></li></ol></pre> **示例 2:** <pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><p><code><span class="pun"> 输入:</span><span class="pln">nums </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="lit">0</span><span class="pun">]</span></code></p></li><li class="L1" data-node-id="20210516220557-zxyy58l"><p><code><span class="pun"> 输出:</span><span class="lit">0</span></code></p></li></ol></pre> **示例 3:** <pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><p><code><span class="pun"> 输入:</span><span class="pln">nums </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="lit">2</span><span class="pun">,</span><span class="lit">4</span><span class="pun">]</span></code></p></li><li class="L1" data-node-id="20210516220557-laycvh5"><p><code><span class="pun"> 输出:</span><span class="lit">6</span></code></p></li></ol></pre> **示例 4:** <pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><p><code><span class="pun"> 输入:</span><span class="pln">nums </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="lit">8</span><span class="pun">,</span><span class="lit">10</span><span class="pun">,</span><span class="lit">2</span><span class="pun">]</span></code></p></li><li class="L1" data-node-id="20210516220557-9vsnht7"><p><code><span class="pun"> 输出:</span><span class="lit">10</span></code></p></li></ol></pre> **示例 5:** <pre class="prettyprint linenums prettyprinted"><ol class="linenums"><li class="L0"><p><code><span class="pun"> 输入:</span><span class="pln">nums </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="lit">14</span><span class="pun">,</span><span class="lit">70</span><span class="pun">,</span><span class="lit">53</span><span class="pun">,</span><span class="lit">83</span><span class="pun">,</span><span class="lit">49</span><span class="pun">,</span><span class="lit">91</span><span class="pun">,</span><span class="lit">36</span><span class="pun">,</span><span class="lit">80</span><span class="pun">,</span><span class="lit">92</span><span class="pun">,</span><span class="lit">51</span><span class="pun">,</span><span class="lit">66</span><span class="pun">,</span><span class="lit">70</span><span class="pun">]</span></code></p></li><li class="L1" data-node-id="20210516220557-c0x3icf"><p><code><span class="pun"> 输出:</span><span class="lit">127</span></code></p></li></ol></pre> **提示:** * `1 <= nums.length <= 2 * 104` * `0 <= nums[i] <= 231 - 1` ### 题解 暴力的二重遍历可以解答,但是会超出时间显示。力扣的题解都是通过二进制实现,看不懂,放弃。查了另外大佬的题解发现看懂的方法,利用剪枝的方法减少时间复杂度。原理是:两数异或的最大值不会超过两个数相加,但是这种方法如果都是统一数据的情况,时间复杂度依旧是 O(N*N) [大佬题解链接](https://leetcode-cn.com/problems/maximum-xor-of-two-numbers-in-an-array/solution/c-pai-xu-jia-jian-zhi-jian-dan-shi-xian-qj8su/) [力扣题解](https://leetcode-cn.com/problems/maximum-xor-of-two-numbers-in-an-array/solution/shu-zu-zhong-liang-ge-shu-de-zui-da-yi-h-n9m9/) ### 代码 ```cpp class Solution { public: int findMaximumXOR(vector<int>& nums) { sort(nums.begin(), nums.end()); int n = nums.size(); long long maxValue = 0; for (int i = n -1; i >= 1; i--) { for (int j = i -1; j >= 0; j--) { if ((long long)(nums[i]) + nums[j] < maxValue) { break; } maxValue = max(maxValue, (long long)(nums[i] ^ nums[j])); } } return maxValue; } }; ```
ty
2021年5月16日 22:18
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
阅读量
次
本站总访问量
次
本站访客数
人次
Markdown文件
分享
链接
类型
密码
更新密码