当前位置:网站首页>【剑指 Offer 】56 - II. 数组中数字出现的次数 II
【剑指 Offer 】56 - II. 数组中数字出现的次数 II
2022-07-02 21:32:00 【LuZhouShiLi】
剑指 Offer 56 - II. 数组中数字出现的次数 II
题目
在一个数组 nums 中除一个数字只出现一次之外,其他数字都出现了三次。请找出那个只出现一次的数字
思路
将数组中所有的数字的二进制表示的每一位都加起来,如果某一位的和能被3正处,那么那个只出现一次的数字二进制表示中对应的那一位就是0否则就是1。
代码
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])
{
// 得到各个二进制位为1的有多少个
bits[j] += nums[i] % 2;
nums[i] /= 2;
j++;
}
}
int ans = 0;
for(int i = 0; i < 32; i++)
{
// 利用%3 来秋的对应位置上有没有1 有的话乘以对应的2的i次方
ans += (1 << i) * (bits[i] % 3);
}
return ans;
}
};
边栏推荐
- Write the content into the picture with type or echo and view it with WinHex
- qwb2018_ core kernel_ rop
- kernel_ uaf
- MySQL learning notes (Advanced)
- Analysis of enterprise financial statements [3]
- Research Report on minimally invasive medical robot industry - market status analysis and development prospect prediction
- China plastic bottle and container market trend report, technological innovation and market forecast
- [question brushing diary] classic questions of dynamic planning
- Spend more time with your computer on this special holiday, HHH
- MySQL learning record (9)
猜你喜欢
![The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article](/img/8f/6759b4685a129f9d10d6ea1dc8e61e.jpg)
The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article
![[question brushing diary] classic questions of dynamic planning](/img/31/fcd8230f809d6178f11e7095c1ef94.jpg)
[question brushing diary] classic questions of dynamic planning

Structure array, pointer and function and application cases

MySQL learning notes (Advanced)

I drew a Gu ailing with characters!

26 FPS video super-resolution model DAP! Output 720p Video Online

Off chip ADC commissioning record

MySQL learning record (8)
![[shutter] shutter layout component (Introduction to layout component | row component | column component | sizedbox component | clipoval component)](/img/45/735431f59a84e9554225a72a551ab8.jpg)
[shutter] shutter layout component (Introduction to layout component | row component | column component | sizedbox component | clipoval component)
![[error record] the command line creates an error pub get failed (server unavailable) -- attempting retry 1 in 1 second](/img/6e/c82ff02a249b5d275a4589120a197a.jpg)
[error record] the command line creates an error pub get failed (server unavailable) -- attempting retry 1 in 1 second
随机推荐
Construction and maintenance of business websites [9]
MySQL learning record (5)
Centos7 installation and configuration of redis database
MySQL learning record (2)
Free open source web version of xshell [congratulations on a happy new year]
A river of spring water flows eastward
[shutter] statefulwidget component (pageview component)
Construction and maintenance of business websites [7]
MySQL learning record (9)
Market trend report, technical innovation and market forecast of China's Micro pliers
Construction and maintenance of business website [3]
Sword finger offer (II) -- search in two-dimensional array
Analysis of enterprise financial statements [1]
AES encryption CBC mode pkcs7padding filling Base64 encoding key 32byte iv16byte
kernel tty_ struct
Etcd Raft 协议
Welfare, let me introduce you to someone
ctf-HCTF-Final-Misc200
beginning
China's crude oil heater market trend report, technological innovation and market forecast