当前位置:网站首页>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 .
边栏推荐
- Cloud native monitoring practice (2) monitoring and collection of components outside the TKE cluster
- [2021 taac & Ti-One] frequently asked questions related to Ti-One products
- Jmeter+grafana+influxdb build a visual performance test monitoring platform
- EasyNVR使用Onvif探测设备失败,显示“无数据”是什么原因?
- 03. Tencent cloud IOT device side learning -- overview of mqtt control package
- 这个巡检平台你还不知道,真是亏大了!
- Why do you develop middleware when you are young? "You can choose your own way"
- TRCT test cloud + article online speed
- H265 video streaming web page without plug-in player easywasmlayer Troubleshooting and solution of JS unable to set cover photo
- 1. Leveldb getting started
猜你喜欢

Etching process flow for PCB fabrication
Using consistent hash algorithm in Presto to enhance the data cache locality of dynamic clusters
Using flex to implement common layouts
Issue 39: MySQL time class partition write SQL considerations

How to decompile APK files

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
About swagger

NVM download, installation and use

How to create simple shapes in illustrator 2022
SQL basic tutorial (learning notes)
随机推荐
Error reported after NPM I
Litamin: SLAM Based on geometric approximation of normal distribution
持续助力企业数字化转型-TCE获得国内首批数字化可信服务平台认证
Explore cloudera manager management software tuning (1)
Meituan financial report: making money while burning money
Cloud native monitoring configuration self built alertmanager to realize alarm
专有云TCE COS新一代存储引擎YottaStore介绍
Kubernetes 1.20.5 helm installation Jenkins
布隆过滤器综述文章论文阅读:Optimizing Bloom Filter: Challenges, Solutions, and Comparisons
Tensor and tensor network background and significance - basic knowledge
Classic examples of C language 100
Customizing security groups using BPF
H265 video streaming web page without plug-in player easywasmlayer Troubleshooting and solution of JS unable to set cover photo
Noi Mathematics: solution of quadratic congruence equation
Install MySQL using Yum for Linux
How to build RTSP test URL in Intranet Environment
When the game meets NFT, is it "chicken ribs" or "chicken legs"?
03. Tencent cloud IOT device side learning -- overview of mqtt control package
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
Common GCC__ attribute__