【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 ...
随机推荐
- note4
- 【leetcode】623. Add One Row to Tree
题目如下: Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with ...
- 计算机网络体系之OSI模型
1.计算机网络体系结构 计算机网络体系结构指的是计算机网络层次模型和各层协议的集合.计算机网络按照高度结构化设计方法采用功能分层原理来实现. 2.OSI模型 网络协议是计算机网络必不可少的,一个完整的 ...
- Codeforces 814C - An impassioned circulation of affection
原题链接:http://codeforces.com/contest/814/problem/C 题意:有长度为n的一个字符串,q个询问,每个询问由数字m和字符c组成,问最多在字符串中替换m个字符,使 ...
- flutter Container组件和Text组件
在开始之前,我们先写一个最简单的入口文件: 后面,都是在这个结构的基础上面完成的. 由于Container组件和Text组件都是写在body里面的,所以下面,先将body抽离成一个组件的形式. ...
- R中unlist函数的使用
买的书里面实例讲的不清不楚,所以看帮助文档了 用法:unlist(x, recursive = TRUE, use.names = TRUE) 帮助文档讲x可以是向量或者列表,如果是向量,则原样返回, ...
- 登录成功后如何利用cookie保持登录状态
Cookie是一种服务器发送给浏览器的一组数据,用于浏览器跟踪用户,并访问服务器时保持登录状态等功能. 通常用户登录的时候,服务器根据用户名和密码在服务器数据库中校验该用户是否正确,校验正确后则可以根 ...
- git私有仓库提交代码
#首次提交 #克隆版本库到本地 git clone http://192.168.3.107:9002/develop/zhong.git cd zhong #创建忽略文件(忽略文件自行编辑) tou ...
- composer 国内加速,修改镜像源
为什么慢 由于默认情况下执行 composer 各种命令是去国外的 composer 官方镜像源获取需要安装的具体软件信息,所以在不使用代理.不翻墙的情况下,从国内访问国外服务器的速度相对比较慢 如何 ...
- 测开之路三十:Flask基础之jinja2模板继承
实现某些位置的内容固定,某些位置的内容动态展示,如: 中文文档地址:http://docs.jinkan.org/docs/jinja2/templates.html#template-inherit ...