当前位置:网站首页>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 .
边栏推荐
- Use BPF to count network traffic
- Quickly build MySQL million level test data
- Radiology: contralateral preoperative resting state MRI functional network integration is related to the surgical results of temporal lobe epilepsy
- How much does it cost to develop a small adoption program similar to QQ farm?
- A comprehensive understanding of fiber to home FTTH and optical splitter
- Coding enhances security vulnerability scanning capability and helps the team "move left safely"
- Will the easycvr video channel of the urban intelligent video monitoring image analysis platform occupy bandwidth after stopping playing?
- [kotlin] constructor summary
- Use cloud development to make a login free resource navigation applet!
- Noi Mathematics: solution of quadratic congruence equation
猜你喜欢

How to decompile APK files

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

How to create simple shapes in illustrator 2022
Using consistent hash algorithm in Presto to enhance the data cache locality of dynamic clusters

LC 300. Longest increasing subsequence
SQL basic tutorial (learning notes)
Issue 39: MySQL time class partition write SQL considerations

Etching process flow for PCB fabrication

NVM download, installation and use

The 'ng' entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Check the spelling of the name. If you include a path, make sure the path is correct, and then
随机推荐
[version upgrade] Tencent cloud firewall version 2.1.0 was officially released!
VBA Daniel used the nested loop
test
H265 video streaming web page without plug-in player easywasmlayer Troubleshooting and solution of JS unable to set cover photo
Common GCC__ attribute__
Erc-20 Standard Specification
How to learn go language happily? Let's go!
FPGA systematic learning notes serialization_ Day9 [serial port printing of PS terminal of Xilinx zynq7000 series]
"Gambler" bubble Matt turns around
With the solution, the nickname of the applet suddenly becomes "wechat user", and the avatar cannot be displayed?
Best practices for H5 page adaptation and wechat default font size
Fragment usage
Users of the Tiktok open platform are authorized to obtain the user's fan statistics and short video data
Use py-mysql2pgsql to synchronize MySQL data to Greenplum
[MySQL practice] binlog, a sharp tool for problem analysis
How to decompile APK files
Customizing security groups using BPF
LC 300. Longest increasing subsequence
究竟有哪些劵商推荐?现在网上开户安全么?
QQ domain name detection API interface sharing (with internal access automatic jump PHP code)