当前位置:网站首页>[Jianzhi offer] 56 - ii Number of occurrences of numbers in the array II
[Jianzhi offer] 56 - ii Number of occurrences of numbers in the array II
2022-07-02 21:44:00 【LuZhouShiLi】
The finger of the sword Offer 56 - II. The number of occurrences of numbers in an array II
subject
In an array nums Except that a number appears only once , The other numbers appear three times . Please find the number that only appears once
Ideas
Add up every bit of the binary representation of all the numbers in the array , If someone's sum can be 3 Right place , Then the corresponding bit in the binary representation of the number that only appears once is 0 Otherwise, it would be 1.
Code
class Solution {
public:
int singleNumber(vector<int>& nums) {
int bits[32] = {
0};
for(int i = 0; i < nums.size(); i++)
{
int j = 0;
while(nums[i])
{
// Each binary bit is 1 How many
bits[j] += nums[i] % 2;
nums[i] /= 2;
j++;
}
}
int ans = 0;
for(int i = 0; i < 32; i++)
{
// utilize %3 Is there any corresponding position of Lai Qiu 1 If yes, multiply by the corresponding 2 Of i Power
ans += (1 << i) * (bits[i] % 3);
}
return ans;
}
};
边栏推荐
- Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source
- MySQL learning record (1)
- [shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)
- [shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)
- Welfare, let me introduce you to someone
- Construction and maintenance of business website [1]
- MySQL learning notes (Advanced)
- Technical solution of vision and manipulator calibration system
- Research Report on market supply and demand and strategy of China's right-hand outward rotation entry door industry
- B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
猜你喜欢
Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme
Today, I met a Alipay and took out 35K. It's really sandpaper to wipe my ass. it's a show for me
Redis分布式锁故障,我忍不住想爆粗...
[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)
MySQL learning record (9)
One week dynamics of dragon lizard community | 2.07-2.13
D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)
Basic IO interface technology - microcomputer Chapter 7 Notes
如何访问kubernetes API?
Gbase8s database type
随机推荐
China plastic bottle market trend report, technological innovation and market forecast
treevalue——Master Nested Data Like Tensor
China plastic box market trend report, technological innovation and market forecast
地理探测器原理介绍
Cloud computing technology [1]
Micro SD Card Industry Research Report - market status analysis and development prospect forecast
Research Report on market supply and demand and strategy of China's right-hand outward rotation entry door industry
Free open source web version of xshell [congratulations on a happy new year]
Physical layer cables and equipment
MySQL learning record (9)
How to prevent your jar from being decompiled?
beginning
[shutter] shutter layout component (physicalmodel component)
[dynamic planning] p1220: interval DP: turn off the street lights
Investment strategy analysis of China's electronic information manufacturing industry and forecast report on the demand outlook of the 14th five year plan 2022-2028 Edition
3DES (deSede) encryption CBC mode pkcs7padding filling Base64 encoding key 24byte iv8byte
China's log saw blade market trend report, technological innovation and market forecast
pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
Five message formats of OSPF
Redis distributed lock failure, I can't help but want to burst