热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MaximumXORofTwoNumbersinanArray

MaximumXORofTwoNumbersinanArrayGivenanon-emptyarrayofnumbers,a0,a1,a2,…,an-1,where0≤ai

  1. Maximum XOR of Two Numbers in an Array

Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai <231.

Find the maximum result of ai XOR aj, where 0 ≤ i, j

Could you do this in O(n) runtime?

Example:

Input: [3, 10, 5, 25, 2, 8]

Output: 28

Explanation: The maximum result is 5 ^ 25 &#61; 28.

我的代码

class Solution {public int findMaximumXOR(int[] nums) {if (nums.length &#61;&#61; 0) {return 0;}TrieNode root &#61; new TrieNode();// 建树// 每一个数字循环32次&#xff0c;反正输入没有负数// 从最高位开始处理for (int i : nums) {TrieNode node &#61; root;for (int j &#61; 31; j >&#61; 0; j--) {int bit &#61; (i >> j) & 1;if (bit &#61;&#61; 1) {if (node.one &#61;&#61; null) {// one的位置是null&#xff0c;就新建node.one &#61; new TrieNode();}node &#61; node.one;} else {if (node.zero &#61;&#61; null) {node.zero &#61; new TrieNode();}node &#61; node.zero;}}}int max &#61; Integer.MIN_VALUE;//找每一个数字和别的数字异或出来的最大值&#xff01;//所有最大值&#xff0c;取其中最大的for (int i : nums) {TrieNode node &#61; root;int XOR &#61; 0;// 异或结果,的累加。按位累加也是牛逼for (int j &#61; 31; j >&#61; 0; j--) {int bit &#61; (i >> j) & 1;//如果这一位是1&#xff0c;为了异或最大&#xff0c;最好使用0&#xff0c;这样异或出来是1//如果0不存在就只能使用1&#xff0c;这样这一位异或出来就是0//如果这一位是0&#xff0c;为了异或最大&#xff0c;最好使用1&#xff0c;这样异或出来是1//如果1不存在就只能使用0&#xff0c;这样这一位异或出来就是0if (bit &#61;&#61; 1) {if (node.zero !&#61; null) {node &#61; node.zero;XOR &#43;&#61; 1 < XOR ? max : XOR;}return max;}
}class TrieNode {TrieNode zero;TrieNode one;
}

同样思路
trick: 通过预处理得到最大值的首位1&#xff0c;优化了Trie树的深度。

class Solution {TreeNode root &#61; new TreeNode(0);public int findMaximumXOR(int[] nums) {int max &#61; findMax(nums);int maxBit &#61; 32 - Integer.numberOfLeadingZeros(max);//build prefix treeTreeNode currNode &#61; root;for (int i &#61; 0; i &#61;0 ; j--) {int tmp &#61; (nums[i] >>j)& 1;if(tmp &#61;&#61; 0){if(currNode.left &#61;&#61; null){currNode.left &#61; new TreeNode(0);}currNode &#61; currNode.left;}else {if(currNode.right &#61;&#61; null){currNode.right &#61; new TreeNode(1);}currNode &#61; currNode.right;}}// finished for nums[i], will do it for nums[i&#43;&#43;];currNode &#61; root;}//find max xor valueint maxXOR &#61; 0;for (int i &#61; 0; i &#61;0; j--) {int tmp &#61; (nums[i] >>j)& 1;if(currNode.left !&#61; null && currNode.right !&#61; null){if(tmp &#61;&#61; 0){currNode &#61; currNode.right;}else {currNode &#61; currNode.left;}}else {currNode &#61; currNode.left &#61;&#61; null ? currNode.right : currNode.left;}res &#43;&#61; (tmp^currNode.val) < res ? maxXOR : res;}return maxXOR;}private int findMax(int[] nums){int max &#61; nums[0];for (int i &#61; 1; i max){max &#61; nums[i];}}return max;}private static class TreeNode{int val;TreeNode left,right;TreeNode(int val){this.val &#61; val;}}
}

Bit maniputaipon
这个解法也是利用前缀树的思想&#xff0c;利用Map保存下一次遍历的前缀。&#xff08;空间换时间&#xff09;
贪心思想&#xff0c;从最大的bit到最小的bit组装最大异或值。
借用一段解释&#xff1a;
I think most people who find it hard to understand the code is stuck on this line if(set.contains(tmp ^ prefix))
The tricky part here is that we need to be aware of a key property of XOR applying on the above line: if A ^ B &#61; C, then A ^ B ^ B &#61; C ^ B, then A &#61; C ^ B
Before executing that line, max stands for the maximum we can get if we consider only the most significant i - 1 bits, tmp stands for the potential max value we can get when considering the most significant i bits. How can we get this tmp? The only way we can get this value is that we have two values A and B in the set (a set of most significant i bits of each member), such that A ^ B equals to tmp. As mentioned earlier, A ^ B &#61; tmp is equivalent to A &#61; tmp ^ B. Here is where that line comes in: set.contains(tmp ^ B).

public class Solution {public int findMaximumXOR(int[] nums) {int max &#61; 0, mask &#61; 0;for(int i &#61; 31; i >&#61; 0; i--){mask &#61; mask | (1 < set &#61; new HashSet<>();for(int num : nums){set.add(num & mask);}int tmp &#61; max | (1 <}

推荐阅读
  • 深度强化学习Policy Gradient基本实现
    全文共2543个字,2张图,预计阅读时间15分钟。基于值的强化学习算法的基本思想是根据当前的状态,计算采取每个动作的价值,然 ... [详细]
  • 使用ffmpeg进行视频格式转换的简单例子2006-12-1623:12主要参考FFMPEG里面的apiexample.c以及output_example.c编写intmain(in ... [详细]
  • MapReduce统计每个用户的使用总流量
    1、原始数据2、使用java程序1)新建项目2)导包  hadoop-2.7.3\share\hadoop\mapreducehsfs的那些包commo ... [详细]
  • ForesightNews整理了ETHDenver2023日程及其周边活动供读者参考。 整理: ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 每次用到v-charts我都一阵头疼,因为明明是相同的功能,但是我好像每次用到的解决方法都不一样??每次都是在api中各种查,各种尝试…直到做了个各种数据图形的需求,决定还是好好整 ... [详细]
  • iOS Auto Layout Demystify
    BookDescripterAutoLayouttransformsthewayyoucreateiOSuserinterfaces.Asflexibleasitispowerfu ... [详细]
  • 最强python编辑器,play首发!
    听说c4droid的作者与pydroid ... [详细]
  • 看完这篇还搞不懂HTTPS,就来找我!
    本文将为大家详细梳理一下H ... [详细]
  • 就我个人在学习Python的过程中,经常会出现学习了新方法后,如果隔上几天不用,就忘了的情况,或者刚学习的更好的方法没有得到 ... [详细]
  • 开发笔记:Java多线程深度探索
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Java多线程深度探索相关的知识,希望对你有一定的参考价值。 ... [详细]
  • Mask-RCNN源码阅读笔记
    阅读了https:blog.csdn.netu011974639articledetails78483779?locationNum9&fps1这篇博客这篇博客介 ... [详细]
  • 1、获取类身上的成员变量--class_copyIvarListif([badgeViewChildisKindOfClass:NSClassFromString(_UIBadg ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • 也就是|小窗_卷积的特征提取与参数计算
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了卷积的特征提取与参数计算相关的知识,希望对你有一定的参考价值。Dense和Conv2D根本区别在于,Den ... [详细]
author-avatar
christinezzy850
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有