当前位置:网站首页>421. Maximum XOR value of two numbers in the array
421. Maximum XOR value of two numbers in the array
2022-07-26 13:34:00 【Sun_ Sky_ Sea】
421. The maximum exclusive or value of two numbers in an array
Original title link :https://leetcode.cn/problems/maximum-xor-of-two-numbers-in-an-array/
Give you an array of integers nums , return nums[i] XOR nums[j] The largest operation result of , among 0 ≤ i ≤ j < n .
Advanced : You can O(n) Is it time to solve this problem ?
Example 1:
Input :nums = [3,10,5,25,2,8]
Output :28
explain : The maximum result is 5 XOR 25 = 28.
Example 2:
Input :nums = [0]
Output :0
Example 3:
Input :nums = [2,4]
Output :6
Example 4:
Input :nums = [8,10,2]
Output :10
Example 5:
Input :nums = [14,70,53,83,49,91,36,80,92,51,66,70]
Output :127
Tips :
1 <= nums.length <= 2 * 105
0 <= nums[i] <= 231 - 1
Their thinking :
This problem is a bit operation , And apply a law : If a ^ b = max establish ,max Indicates the current “ Maximum ”, Then there must be max ^ b = a establish . See code for specific implementation .
Code implementation :
class Solution:
def findMaximumXOR(self, nums: List[int]) -> int:
ans = 0
# Mask , Bit operation , Used to get prefix , Initialize to 0
mask = 0
# because 0 <= nums[i] <= 2 Of 31 Power - 1
# loop 31 Next time , To get the maximum , So start from the highest position
for i in range(30, -1, -1):
# Get the mask from the high bit
mask |= (1 << i)
# Put prefixes into a hash table
s = set()
# Prefix is obtained by bitwise and operation through mask and number itself
# Prefix saved to set Used in the following XOR result search
for num in nums:
s.add(mask & num)
# Suppose that the largest XOR value starts from the high order 1
# So use ans To press or operate , Get a temporary XOR maximum temp
# Go to set Find whether it is in num There is a number in , and temp To perform exclusive or operations
# Make the current bit 1
temp = ans | (1 << i)
# Then according to the law : If a ^ b = max establish ,max Indicates the current “ Maximum ”,
# Then there must be max ^ b = a establish
# here temp As a max,prefix As a b,s The value in is regarded as a
for prefix in s:
if temp ^ prefix in s:
# Update XOR Max
ans = temp
break
# Clear this prefix
s.clear()
return ans
reference :
https://leetcode.cn/problems/maximum-xor-of-two-numbers-in-an-array/solution/li-yong-yi-huo-yun-suan-de-xing-zhi-tan-xin-suan-f/
边栏推荐
- The use of asynchronous thread pool in development
- AI theory knowledge map 1 Foundation
- Codeforces Round #810 (Div. 2)【比赛记录】
- [oauth2] VII. Wechat oauth2 authorized login
- Comparison between SIGMOD function and softmax function
- LeetCode 1523. 在区间范围内统计奇数数目
- B+ tree index use (6) leftmost principle -- MySQL from entry to proficiency (18)
- Tianjin emergency response Bureau and central enterprises in Tianjin signed an agreement to deepen the construction of emergency linkage mechanism
- This article explains the FS file module and path module in nodejs in detail
- 【黑马早报】字节旗下多款APP下架;三只松鼠脱氧剂泄露致孕妇误食;CBA公司向B站索赔4.06亿;马斯克否认与谷歌创始人妻子有婚外情...
猜你喜欢

panic: Error 1045: Access denied for user ‘root‘@‘117.61.242.215‘ (using password: YES)

8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇

AI theory knowledge map 1 Foundation

银行业客户体验管理现状与优化策略分析

How to face scientific and technological unemployment?

panic: Error 1045: Access denied for user ‘root‘@‘117.61.242.215‘ (using password: YES)

Learn about Pinia state getters actions plugins

JSON data transfer parameters & date type parameter transfer

Unity中序列化类为json格式

One stroke problem (Chinese postman problem)
随机推荐
Abstract factory and its improvement examples
解决远程主机无法连接mysql数据库的问题
LeetCode 2119. 反转两次的数字
一笔画问题(中国邮递员问题)
How to remove black edges from hyperimage images (two methods)
Photoshop (cc2020) unfinished
MySQL data directory (3) -- table data structure MyISAM (XXVI)
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
Precautions for triggering pytest.main() from other files
[flower carving hands-on] fun music visualization series small project (12) -- meter tube fast rhythm light
【花雕动手做】有趣好玩的音乐可视化系列小项目(12)---米管快速节奏灯
云智技术论坛工业专场 明天见!
Can I take your subdomain? Exploring Same-Site Attacks in the Modern Web
飞盘,2022年“黑红”顶流
Ultimate doll 2.0 | cloud native delivery package
Parent class reference to child class (parent class reference points to child class object)
HCIP第十一天比较(BGP的配置、发布)
LeetCode 217. 存在重复元素
【OAuth2】八、OAuth2登录的配置逻辑-OAuth2LoginConfigurer和OAuth2ClientConfigurer
[turn] judge the relationship between two geometries in ArcGIS