当前位置:网站首页>Application of XOR. (extract the rightmost 1 in the number, which is often used in interviews)
Application of XOR. (extract the rightmost 1 in the number, which is often used in interviews)
2022-06-28 07:33:00 【Kinght_ one hundred and twenty-three】

We all know , In computer system , All numbers are stored in binary , Then we need to extract the rightmost of the numbers 1 What to do ?
One 、 background
The application of XOR is very important . For example, when we interview , The interviewer asked a question : In a bunch of numbers , Only two numbers appear an odd number of times , Other numbers appear an even number of times , Ask for odd numbers a and b, Time complexity required O(N), Extra space complexity O(1).
Two 、 Their thinking
First , We need an XOR array , The result is a^b, And then what ?
then a^b Definitely not for 0 Of , We need to know a perhaps b The result can be calculated by the value of , So how do you know a perhaps b Well ?
a and b In the form of computer storage, it is stored in binary form , Again because a It must not be equal to b, So we know a and b One of them is definitely different . Take advantage of this feature , Suppose we find a different bit on the far right ( That is to say a The digit of is 0, and b The digit of is 1, Or vice versa ), How do we find the rightmost 1 Well ?
right_one = arr & (~arr + 1) # Extract the rightmost 1
This line of code is a good way to find the rightmost 1, Let me explain , First arr The value is a^b Result , So let's reverse the result , Then add 1, At this time arr Rightmost of 1 That's the answer. .
such as arr The result is 000111001100. After taking the reverse, it is 111000110011, add 1 For after 111000110100, Then the rightmost 1 Is the final answer .
After finding it , We can use this to iterate through the array again , The result is a perhaps b, Then let the result XOR arr, So you get two numbers .
3、 ... and 、 Complete code
nums = [10, 10, 10, 5, 6, 6, 7, 7, 8, 8, 9, 9, 55, 55, 66, 66, 33, 33, 5, 888, 888, 888]
arr = 0
for i in range(len(nums)):
arr ^= nums[i] # At this time arr by a^b
right_one = arr & (~arr + 1) # Extract the rightmost 1
eor = 0
for i in range(len(nums)):
if nums[i] & right_one == 0:
eor ^= nums[i]
print(f' The two numbers are {
eor} and {
arr^eor}')
Output :
The two numbers are 888 and 10
边栏推荐
- Pfizer's new Guankou medicine has entered the Chinese market, and the listing of relevant products of domestic pharmaceutical enterprises is just around the corner
- Comment la passerelle BACnet / IP recueille - t - elle les données du système central de contrôle des bâtiments?
- Face to face experience --- test engineer web side automation --- interview questions for large factories
- 代码提交规范
- Introduction and several months' experience of extending the solution thanos of Prometheus
- XML序列化向后兼容
- MMR重排(相似度通过编辑距离和重复度计算)
- Mysql8.0 and mysql5.0 accessing JDBC connections
- 云原生(待更新)
- Is it safe for flush to open an account online
猜你喜欢

2021 VDC: technological architecture evolution of vivo Internet service for 100 million users | PPT download attached

kubernetes部署thanos ruler的发送重复告警的一个隐秘的坑

什么是一致性哈希?可以应用在哪些场景?

What is EC blower fan?

Introduction and several months' experience of extending the solution thanos of Prometheus

Cloud native (to be updated)

MySQL installation steps - installing MySQL on Linux (3)

Comprehensive analysis of real enterprise software testing process

Block transmission by golang gin framework
![[thanos source code analysis series]thanos query component source code analysis](/img/e4/2a87ef0d5cee0cc1c1e1b91b6fd4af.png)
[thanos source code analysis series]thanos query component source code analysis
随机推荐
Design and implementation of spark offline development framework
R language ggmap visual cluster
"Jay bear" plummeted by 96.6%. Why is NFT with star goods cold?
Top 25 most popular articles on vivo Internet technology in 2021
BACnet/IP網關如何采集樓宇集中控制系統數據
Libuv framework echo server C source code explanation (TCP part)
LLVM 与 Clang
自律挑战30天
同花顺网上开户安全吗
The practice of traffic and data isolation in vivo Reviews
Tencent continued to lay off staff in the second half of the year, and all business groups reduced by at least 10%. What do you think of this? Followers
R语言绘制 ggplot2 季节性图
Application and Optimization Practice of redis in vivo push platform
以动态规划的方式求解最长回文子串
Construction and exploration of vivo database and storage platform
This keyword details
Ice, protobuf, thrift -- Notes
金山云团队分享 | 5000字读懂Presto如何与Alluxio搭配
Jinshan cloud team shared | 5000 words to understand how Presto matches with alluxio
Detailed explanation of collection class methods____ (4) Judgment and assignment, etc