当前位置:网站首页>【剑指 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;
}
};
边栏推荐
- Etcd Raft 协议
- Today, I met a Alipay and took out 35K. It's really sandpaper to wipe my ass. it's a show for me
- China microporous membrane filtration market trend report, technological innovation and market forecast
- qwb2018_ core kernel_ rop
- Go language learning summary (5) -- Summary of go learning notes
- Sword finger offer (II) -- search in two-dimensional array
- 读博士吧,研究奶牛的那种!鲁汶大学 Livestock Technology 组博士招生,牛奶质量监测...
- Analysis of enterprise financial statements [4]
- Summary of the first week of summer vacation
- MySQL learning notes (Advanced)
猜你喜欢
![[shutter] the shutter plug-in is used in the shutter project (shutter plug-in management platform | search shutter plug-in | install shutter plug-in | use shutter plug-in)](/img/80/215499c66243d5a4453d8e6206c012.jpg)
[shutter] the shutter plug-in is used in the shutter project (shutter plug-in management platform | search shutter plug-in | install shutter plug-in | use shutter plug-in)

Gbase8s database type

Welfare, let me introduce you to someone

MySQL learning record (6)

MySQL learning record (5)

How to prevent your jar from being decompiled?

读博士吧,研究奶牛的那种!鲁汶大学 Livestock Technology 组博士招生,牛奶质量监测...

The neo4j skill tree was officially released to help you easily master the neo4j map database

发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...

Spend more time with your computer on this special holiday, HHH
随机推荐
Research Report on market supply and demand and strategy of Chinese garden equipment industry
mysql
Common authority query instructions in Oracle
beginning
26 FPS video super-resolution model DAP! Output 720p Video Online
Plastic floating dock Industry Research Report - market status analysis and development prospect forecast
Construction and maintenance of business websites [4]
Add two numbers of leetcode
Browser - clean up the cache of JS in the page
MySQL learning record (8)
China plastic box market trend report, technological innovation and market forecast
[shutter] shutter layout component (physicalmodel component)
Sword finger offer (I) -- handwriting singleton mode
Free open source web version of xshell [congratulations on a happy new year]
Analysis of enterprise financial statements [2]
Construction and maintenance of business websites [7]
Accounting regulations and professional ethics [19]
MySQL learning notes (Advanced)
基本IO接口技术——微机第七章笔记
Analysis of enterprise financial statements [1]