当前位置:网站首页>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/
边栏推荐
- JS object assignment problem
- Emotion analysis model based on Bert
- LeetCode 217. 存在重复元素
- Square root of leetcode 69. x
- Hcip day 11 comparison (BGP configuration and release)
- Unicode file parsing methods and existing problems
- B+ tree (3) clustered index, secondary index -- MySQL from entry to proficiency (XV)
- 【Oauth2】五、OAuth2LoginAuthenticationFilter
- 【Oauth2】七、微信OAuth2授权登录
- 周伟:寻找非共识性投资机会,陪伴延迟满足的创始团队
猜你喜欢

Team research and development from ants' foraging process (Reprint)

Dimension disaster dimension disaster suspense

Can I take your subdomain? Exploring Same-Site Attacks in the Modern Web

Basic sentence structure of English ----- origin

Jenkins 中 shell 脚本执行失败却不自行退出
![[flower carving hands-on] interesting and fun music visualization series small project (13) -- organic rod column lamp](/img/d4/7b9c7c99d46661e1be2963a342dd18.jpg)
[flower carving hands-on] interesting and fun music visualization series small project (13) -- organic rod column lamp
![[oauth2] VII. Wechat oauth2 authorized login](/img/1a/3f2b9fc57759a1fa3fda1451492e5c.png)
[oauth2] VII. Wechat oauth2 authorized login

如何构建以客户为中心的产品蓝图:来自首席技术官的建议

Ultimate doll 2.0 | cloud native delivery package

Flutter multi-channel packaging operation
随机推荐
B+ tree index use (9) grouping, back to table, overlay index (21)
Golang端口扫描设计
历时15年、拥有5亿用户的飞信,彻底死了
How to face scientific and technological unemployment?
8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
The serialization class in unity is in JSON format
.NET WebAPI 使用 GroupName 对 Controller 分组呈现 Swagger UI
[flower carving hands-on] interesting and fun music visualization series small project (13) -- organic rod column lamp
Sword finger offer (x): rectangular coverage
Win11+vs2019 configuration yolox
Chat system based on webrtc and websocket
[oauth2] VIII. Configuration logic of oauth2 login -oauth2loginconfigurer and oauth2clientconfigurer
AI theory knowledge map 1 Foundation
B+ tree selection index (2) -- MySQL from entry to proficiency (23)
Hcip day 12 notes sorting (BGP Federation, routing rules)
MySQL data directory (2) -- table data structure (XXV)
Can I take your subdomain? Exploring Same-Site Attacks in the Modern Web
[upper computer tutorial] Application of integrated stepping motor and Delta PLC (as228t) under CANopen communication
一笔画问题(中国邮递员问题)
Square root of leetcode 69. x