当前位置:网站首页>【剑指 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;
}
};
边栏推荐
- Research Report on market supply and demand and strategy of microplate instrument industry in China
- How is LinkedList added?
- Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
- 3DES (deSede) encryption CBC mode pkcs7padding filling Base64 encoding key 24byte iv8byte
- Analysis of neural network
- It is said that this year gold three silver four has become gold one silver two..
- MySQL learning record (6)
- Analysis of enterprise financial statements [4]
- Market trend report, technical innovation and market forecast of China's Micro pliers
- Construction and maintenance of business websites [6]
猜你喜欢

Today, I met a Alipay and took out 35K. It's really sandpaper to wipe my ass. it's a show for me

Report on investment development and strategic recommendations of China's vibration isolator market, 2022-2027
![[Yu Yue education] reference materials of analog electronic technology of Nanjing Institute of information technology](/img/2f/bb99836bb3ad725483f30531ff4d53.jpg)
[Yu Yue education] reference materials of analog electronic technology of Nanjing Institute of information technology

D4: unpaired image defogging, self enhancement method based on density and depth decomposition (CVPR 2022)
![[shutter] shutter layout component (fractionallysizedbox component | stack layout component | positioned component)](/img/5f/e96baefd9481c496024fed345e31fe.jpg)
[shutter] shutter layout component (fractionallysizedbox component | stack layout component | positioned component)

Write the content into the picture with type or echo and view it with WinHex

Unexpectedly, there are such sand sculpture code comments! I laughed

How is LinkedList added?

Etcd Raft 协议

Check the confession items of 6 yyds
随机推荐
Construction and maintenance of business website [2]
beginning
China's noise meter market trend report, technical dynamic innovation and market forecast
Gbase 8s database basic syntax
Sword finger offer (I) -- handwriting singleton mode
kernel_ uaf
Hot backup routing protocol (HSRP)
System (hierarchical) clustering method and SPSS implementation
pyqt图片解码 编码后加载图片
Sword finger offer (II) -- search in two-dimensional array
How does esrally perform simple custom performance tests?
pyqt圖片解碼 編碼後加載圖片
Welfare, let me introduce you to someone
Research Report on right-hand front door industry - market status analysis and development prospect forecast
Browser - clean up the cache of JS in the page
Gbase8s database type
Welfare | Pu Aries | liv heart co branded Plush surrounding new products are on the market!
Construction and maintenance of business websites [10]
[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)
Capacity expansion mechanism of ArrayList