【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 ...
随机推荐
- boost array
boost::array is similar to std::array, which was added to the standard library with C++11. With boos ...
- ldd3 编写scull尝试
快速参考: #include <linux/types.h> dev_t dev_t is the type used to represent device numbers within ...
- JS中关于引用类型数据及函数的参数传递
(JavaScript 中,函数的参数传递方式都是按值传递,没有按引用传递的参数) 一.数据类型 在 javascript 中数据类型可以分为两类: 基本类型值 primitive type,比如Un ...
- wangeditor 粘贴word内容带样式
这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用 后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下) ...
- 【Java】时间戳与Date相互转换
时间戳转Date public static void main(String[] args) { // 10位的秒级别的时间戳 long time1 = 1527767665; String res ...
- 【HDOJ6598】Harmonious Army(最小割)
题意:有n个人,每个人可以从A,B两种职业中选择一种 有m对两人组,如果两个人都是A能获得p的收益,一个A一个B能获得q的收益,都是B能获得r的收益,其中q=p/4+r/3,保证p%4=0,r%3=0 ...
- BUUCTF | 高明的黑客
这一题一开始我没有理解"www.tar.gz"的涵义,还以为有一个其他的网站叫这个,后来才突然顿悟他也有可能是一个目录!!!地址栏输入”/www.tar.gz“ 然后就可以得到源码 ...
- python中模块介绍
一,模块概念 在计算机程序开发的过程当中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编码更加容易维护,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码 ...
- Ubuntu编译ruby
要用sass,需要ruby2.0以上版本 ubuntu升级ruby到2.1 1.安装前更新: sudo apt-get -y update sudo apt-get install cmake sud ...
- 建站手册-浏览器信息:苹果 Safari 浏览器
ylbtech-建站手册-浏览器信息:苹果 Safari 浏览器 1.返回顶部 1. http://www.w3school.com.cn/browsers/browsers_safari.asp 2 ...