【leetcode】421. Maximum XOR of Two Numbers in an Array
题目如下:
解题思路:本题的难点在于O(n)的复杂度。为了减少比较的次数,我们可以采用字典树保存输入数组中所有元素的二进制的字符串。接下来就是找出每个元素的异或的最大值,把需要找最大值的元素转成二进制表达后逐位在字典树中查找,查找的时候优先匹配反码,反码不存在则用原码。
代码如下:
class Solution(object):
def findMaximumXOR(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
MAX_LEN = 31
root = {}
for i in nums:
node = root
i = ''*(MAX_LEN-len(bin(i)[2:])) + bin(i)[2:]
for b in i:
if b not in node:
node[b] = {}
node = node[b]
#print root['0']['0']['0']['1']['1']
res = 0
for i in nums:
path = ''
i = '' * (MAX_LEN - len(bin(i)[2:])) + bin(i)[2:]
node = root
for b in i:
if len(node) == 0:
break
ib = '' if b == '' else ''
if ib in node:
node = node[ib]
path += ib
else:
node = node[b]
path += b
#print i,path,int(i,2),int(path,2)
res = max(res,int(i,2)^int(path,2))
return res
【leetcode】421. Maximum XOR of Two Numbers in an Array的更多相关文章
- 【LeetCode】421. Maximum XOR of Two Numbers in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 依次遍历每一位 前缀树 日期 题目地址:https://lee ...
- [LeetCode] 421. 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 re ...
- 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...
- [LeetCode] 421. Maximum XOR of Two Numbers in an Array(位操作)
传送门 Description Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Fin ...
- leetcode 421.Maximum XOR of Two Numbers in an Array
题目中给定若干个数,然后任意选定两个数使得其异或值最大. 先利用样例中的: 3 10 5 25 2 8 这些数转换为二进制来看的话那么是先找到最高位的1然后与数组中其他的数相与后的数值保存到set中去 ...
- 421. Maximum XOR of Two Numbers in an Array——本质:利用trie数据结构查找
Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- 421 Maximum XOR of Two Numbers in an Array 数组中两个数的最大异或值
给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 .找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ≤ i, j < ...
- 421. Maximum XOR of Two Numbers in an Array
这题要求On时间复杂度完成, 第一次做事没什么思路的, 答案网上有不贴了, 总结下这类题的思路. 不局限于这个题, 凡是对于这种给一个 数组, 求出 xxx 最大值的办法, 可能上来默认就是dp, ...
- LeetCode 421. 数组中两个数的最大异或值(Maximum XOR of Two Numbers in an Array) 71
421. 数组中两个数的最大异或值 421. Maximum XOR of Two Numbers in an Array 题目描述 给定一个非空数组,数组中元素为 a0, a1, a2, - , a ...
随机推荐
- https 配置
参考:https://www.cnblogs.com/tanghuachun/p/9951849.html 1.将pfx文件拷贝到application.properties同级目录下 2.添加配置文 ...
- 【leetcode】828. Unique Letter String
题目如下: A character is unique in string S if it occurs exactly once in it. For example, in string S = ...
- 【leetcode】1012. Complement of Base 10 Integer
题目如下: Every non-negative integer N has a binary representation. For example, 5 can be represented a ...
- visual studio 中gitflow使用
vs中已集成了git,但是如果想使用gitflow,要去扩展包里面下载 装完后,在右侧面板的"团队资源管理"就会出现gitflow 进行git flow实始化 后就可以进行 各分支 ...
- Python基础教程(015)--Python2默认不支持中文
前言 Python2默认不支持中文 内容 市场上有Python2,和Python3, Python2的解释器不支持中文. 用Python3来运行文件. 错误信息 SyntaxError:Non-ASC ...
- excle里边的数据怎么导入oracle数据库
方式一:(不正式) select出的列数与已经准备好的excle中的列数相同.select xh,name from 表名 where xh = 'ghf' for update; (查不到任何结 ...
- vue搭配的UI框架 pc端 + 移动端
PC桌面端UI框架: 1,iview (最新,用户评分高功能多炫酷 解决和避免了其他UI框架出现的一些小问题) 2, bootstrap (使用用户最多样式死板没特色) 3,Element ...
- html中map标签和area标签的应用(总结)
html中map标签和area标签的应用(总结) 一.总结 一句话总结: html中map标签和area标签和组成图片地图,在前端优化中可以减少http请求 1.map标签的用途是什么? 图片地图:是 ...
- (四)添加yaffs2文件系统支持
1. 获取yaffs2源码 在linux工作目录下进行clone操作: git clone git://www.aleph1.co.uk/yaffs2 完成后会在当前目录下产生yaffs2的源码目录: ...
- 关于C++中nothrow的某某某
前言 在学习C++中new的种种用法时,在operator new的其中一个重载版本中看一个参数nothrow,想弄清楚到底是什么意思?nothrow顾名思义,就是不抛出的意思嘛!不抛出啥,在C++中 ...