当前位置:网站首页>Number of occurrences of numbers in the array (medium difficulty)
Number of occurrences of numbers in the array (medium difficulty)
2022-06-24 17:38:00 【Nulibiao】
Title Overview ( Medium difficulty )

Topic link :
Point me into the leetcode
Ideas and code
Train of thought display
First of all, this topic uses the original code , Inverse code , Knowledge of complement , If you need help, take a look at this blog and review it :
Click me to enter the blog
See this for the solution :
Click to enter the solution
So let's start with the code , Then I will make a supplement to the explanation of the problem
Code example
class Solution {
public int[] singleNumbers(int[] nums) {
int bitmask = 0;
// XOR all the elements in the array
for (int num : nums) {
bitmask ^= num;
}
// Because the result of XOR operation is not always 2 Of n The next power ,
// There may be more than one in binary 1, For the sake of calculation
// We just need to keep any of them 1, Everything else is
// Let him become 0, What's left here is the one on the far right 1
bitmask &= -bitmask;
int[] rets = {
0, 0};
for (int num : nums) {
// Then divide the array into two parts , Each part is in
// Or, respectively
if ((num & bitmask) == 0) {
rets[0] ^= num;
} else {
rets[1] ^= num;
}
}
return rets;
}
}
Time complexity :O(N)
Spatial complexity :O(1)
analysis :
for (int num : nums) {
bitmask ^= num;
}
Here is the XOR of all the numbers , There must be only the last two numbers that only appear once , For example, the group of figures given in the problem solution :[12,13,14,17,14,12],
When all the numbers in the array are XORed , only 13^17, The result after XOR is 00000000 00000000 00000000 00011100, And one of our code is bitmask &= -bitmask, So at this time bitmask be equal to 00000000 00000000 00000000 00011100, That is to say 28, that (-28) How to calculate? ? Be careful : Complement is the binary representation of negative numbers in the computer , here -28 The original code of is 10000000 00000000 00000000 00011100, The reverse code is 11111111 11111111 11111111 11100011, Then the complement is 11111111 11111111 11111111 11100100, that -bitmask = 11111111 11111111 11111111 11100100, and bitmask &= -bitmask As shown in the figure below :
You can see at this time bitmask The value of is 00000000 00000000 00000000 00000100,
You can see our last 1 Be kept , At this point, our original array can be divided into two groups , One is related to the present bitmask And (&) The result is 0 A set of , Not for 0 A set of ( In fact, this result is not 0 The reason is whether the penultimate number after binary conversion of the number in each array is 1 still 0 of , by 0 Word is 0, by 1 What you say is wrong 0, You can verify it yourself )
So in the end 13 and 17 There must be a different group , then 12 and 14 Each must be in the same group ,12 ^ 12 = 0,14 ^ 14 = 0, So there is only one left in the array 13 and 17, Then return the array .
边栏推荐
- SQL basic tutorial (learning notes)
- Five steps to effectively monitor network traffic
- Common GCC__ attribute__
- Contributed code to famous projects for the first time, a little nervous
- Research on clock synchronization performance monitoring system based on 1588v2 Technology
- 2021-04-02: given a square or rectangular matrix, zigzag printing can be realized.
- Quickly build MySQL million level test data
- 腾讯云TCS:面向应用的一站式PaaS 平台
- Cloud native monitoring via blackbox_ Exporter monitoring website
- A solution to the problem that the separator of WordPress title - is escaped as -
猜你喜欢
Using consistent hash algorithm in Presto to enhance the data cache locality of dynamic clusters
About swagger

Mengyou Technology: tiktok current limiting? Teach you to create popular copywriting + popular background music selection

How to decompile APK files

Etching process flow for PCB fabrication

Constantly changing the emergency dialing of harmonyos ETS during the new year

LC 300. Longest increasing subsequence
SQL basic tutorial (learning notes)

NVM download, installation and use

Error reported after NPM I
随机推荐
[DB Bao 45] MySQL highly available mgr+consult architecture deployment
Using consistent hash algorithm in Presto to enhance the data cache locality of dynamic clusters
Solution to the problem that qlineedit setting qdoublevalidator setting range is invalid
[play with Tencent cloud] check 9 popular Tencent cloud products
Why do you develop middleware when you are young? "You can choose your own way"
这个巡检平台你还不知道,真是亏大了!
VBA Daniel used the nested loop
Research on clock synchronization performance monitoring system based on 1588v2 Technology
[kotlin] constructor summary
How to troubleshoot and solve the problem that the ultra-low delay security live broadcast system webrtc client plays no audio in the browser?
Classic examples of C language 100
This time, talk about the dry goods of industrial Internet | TVP technology closed door meeting
Three simple steps to quickly complete order data processing through workflow (ASW)
Realize business development on behalf of small programs, and 99% restore the function of service category management in the background of official account
Use BPF to count network traffic
Quickly build MySQL million level test data
A comprehensive understanding of fiber to home FTTH and optical splitter
Comparison of similarities and differences between easynvr video edge computing gateway and easynvr software versions
Explanation of pod DNS configuration & cases of DNS resolution failure
Leveldb source code analysis -- writing data