博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode Maximum XOR of Two Numbers in an Array
阅读量:4185 次
发布时间:2019-05-26

本文共 1069 字,大约阅读时间需要 3 分钟。

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 ≤ ij < n.

Could you do this in O(n) runtime?

Example:

Input: [3, 10, 5, 25, 2, 8]Output: 28Explanation: The maximum result is 5 ^ 25 = 28.

----------------------------------------------------------------------------------------

Cycle bit by bit. For every bit, we could know the largest prefix expected. If the expected largest prefix could be xor by some two nums (implemented by hashSet), we could know the final answer. The complexity is O(n)

class Solution:    def findMaximumXOR(self, nums):        pre, mask = 0, 0        for i in range(31, -1, -1):            one = (1 << i)            mask |= one            exist = set()            cur_largest = pre | one            for num in nums:                cur = num & mask                if ((cur ^ cur_largest) in exist):                    pre = cur_largest                else:                    exist.add(cur)        return pres = Solution()print(s.findMaximumXOR([3, 10, 5, 25, 2, 8]))

 

转载地址:http://wtfoi.baihongyu.com/

你可能感兴趣的文章
kudu存储引擎
查看>>
PHP语法1
查看>>
Linux如何查看端口状态
查看>>
Guava cache 缓存
查看>>
UUID.randomUUID()是什么
查看>>
TimeUnit是什么
查看>>
2017年大数据的变化趋势
查看>>
作业、任务、进程、线程的区别
查看>>
laypage分页
查看>>
ojdbc14.jar 与ojdbc6.jar的区别
查看>>
如何区分Oracle的数据库,实例,服务名,SID
查看>>
怎样使用sqlplus连接oracle11g数据库
查看>>
JDBC连接数据库
查看>>
java日志组件介绍(common-logging,log4j,slf4j,logback )
查看>>
java运行jar命令提示没有主清单属性
查看>>
使用Maven为一个项目生成多个Jar包,将一个目录打成jar包
查看>>
CMD命令名详细大全
查看>>
C、C++、MATLAB、Python、Go 哪个比较适合写算法
查看>>
Spring的一个命名空间的名称空间处理程序没有找到
查看>>
Maven常用插件配置和使用
查看>>