当前位置:网站首页>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
边栏推荐
- In idea, the get and set methods may be popular because the Lombok plug-in is not installed
- 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
- LeetCode+ 51 - 55 回溯、动态规划专题
- kubelet驱逐机制的源码分析
- ice, protobuf ,thrift -- 笔记
- SQL statement optimization steps (1)
- 安全培训是员工最大的福利!2022新员工入职安全培训全员篇
- PLC -- 笔记
- The practice of event driven architecture in vivo content platform
- Flutter realizes the function of "shake"
猜你喜欢

Kubernetes deploys a secret pit where thanos ruler sends repeated alarms

How bacnet/ip gateway collects data of building centralized control system

Open62541 import nodeset file directly

Leetcode+ 66 - 70 high precision, two sub topics

Yesterday, I went to a large factory for an interview and asked me to do four arithmetic operations. Fortunately, I am smart enough

Solving the longest palindrome substring by dynamic programming

In idea, the get and set methods may be popular because the Lombok plug-in is not installed

Using interceptor and cache to complete interface anti brushing operation

Jetpack - defects of livedata component and Countermeasures

Spark 离线开发框架设计与实现
随机推荐
Implementation of commit message standardized control in large projects
自动化测试的生命周期是什么?
Understanding of OPC protocol
8 figures | analyze Eureka's first synchronization registry
No suspense about the No. 1 Internet company overtime table
kubelet驱逐机制的源码分析
Modifying MySQL port number under Linux
小小一款代码编辑器竟然也可以有程序运行之功能——Sublime Text3运行各种语言程序的总结
LeetCode+ 51 - 55 回溯、动态规划专题
实时数据库 - 笔记
QT -- communication protocol
华为云计算之物理节点CNA安装教程
XML serialization backward compatible
Practice of traffic recording and playback in vivo
MySQL master-slave replication, detailed configuration, create unable to connect processing prompt
R language Kolmogorov Smirnov tests whether the two samples follow the same distribution.
Mysql8.0和Mysql5.0访问jdbc连接
R 语言 ggmap 可视化集群
R 和 rgl 绘制 3D 结
A small code editor can also run programs -- a summary of sublime Text3 running programs in various languages