当前位置:网站首页>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 .
边栏推荐
- Analysis of signal preemptive scheduling based on go language from source code
- FPGA systematic learning notes serialization_ Day9 [serial port printing of PS terminal of Xilinx zynq7000 series]
- Tiktok Kwai, e-commerce enters the same river
- Use py-mysql2pgsql to synchronize MySQL data to Greenplum
- [play Tencent cloud] experience and development of game multimedia engine (II)
- 腾讯云TCS:面向应用的一站式PaaS 平台
- Install MySQL using Yum for Linux
- EasyNVR使用Onvif探测设备失败,显示“无数据”是什么原因?
- System Verilog - randomize
- 专有云TCE COS新一代存储引擎YottaStore介绍
猜你喜欢

Etching process flow for PCB fabrication

Constantly changing the emergency dialing of harmonyos ETS during the new year
Using flex to implement common layouts
SQL basic tutorial (learning notes)

How to create simple shapes in illustrator 2022

NVM download, installation and use

LC 300. Longest increasing subsequence

Error reported after NPM I

Why do you develop middleware when you are young? "You can choose your own way"

Mengyou Technology: tiktok current limiting? Teach you to create popular copywriting + popular background music selection
随机推荐
FPGA systematic learning notes serialization_ Day10 [sequential logic, competitive adventure, synchronous reset, asynchronous reset]
[version upgrade] Tencent cloud firewall version 2.1.0 was officially released!
视频平台如何将旧数据库导入到新数据库?
About swagger
Mengyou Technology: tiktok current limiting? Teach you to create popular copywriting + popular background music selection
How to create simple shapes in illustrator 2022
Management system permission design
[play Tencent cloud] experience and development of game multimedia engine (II)
A comprehensive understanding of fiber to home FTTH and optical splitter
Zabix5.0-0 - agent2 monitoring MariaDB database (Linux based)
Solutions for RTSP video streaming played by several browsers
布隆过滤器综述文章论文阅读:Optimizing Bloom Filter: Challenges, Solutions, and Comparisons
Mysql database performance testing tool recommendation
Industrial security experts talk about DDoS countermeasures from the perspective of attack and defense
Welcome to the network security threat information sharing program
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
-Bash: wget: command not found
Leveldb source code analysis -- version management
When the game meets NFT, is it "chicken ribs" or "chicken legs"?
Erc-721 Standard Specification