当前位置:网站首页>[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;
}
};
边栏推荐
- Research Report on market supply and demand and strategy of microplate instrument industry in China
- Hot backup routing protocol (HSRP)
- tinymce可视化编辑器增加百度地图插件
- 关于测试用例
- China plastic bottle and container market trend report, technological innovation and market forecast
- Capacity expansion mechanism of ArrayList
- [C language] [sword finger offer article] - replace spaces
- 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
- [shutter] shutter layout component (Introduction to layout component | row component | column component | sizedbox component | clipoval component)
- Research Report on market supply and demand and strategy of China's right-hand outward rotation entry door industry
猜你喜欢
PIP version update timeout - download using domestic image
GEE:(二)对影像进行重采样
【零基础一】Navicat下载链接
Hot backup routing protocol (HSRP)
The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article
pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
[Yu Yue education] reference materials of analog electronic technology of Nanjing Institute of information technology
[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)
MySQL learning record (6)
MySQL learning record (5)
随机推荐
【零基础一】Navicat下载链接
A specially designed loss is used to deal with data sets with unbalanced categories
Baidu sued a company called "Ciba screen"
Accounting regulations and professional ethics [16]
China's log saw blade market trend report, technological innovation and market forecast
Capacity expansion mechanism of ArrayList
关于测试用例
Golang string segmentation
Physical layer cables and equipment
Accounting regulations and professional ethics [17]
2019 Nanchang (relive the classic)
[C language] [sword finger offer article] - replace spaces
Analysis of enterprise financial statements [1]
qwb2018_ core kernel_ rop
Cloud computing technology [2]
[shutter] shutter layout component (physicalmodel component)
How to prevent your jar from being decompiled?
Plastic floating dock Industry Research Report - market status analysis and development prospect forecast
A river of spring water flows eastward
Research Report on right-hand front door industry - market status analysis and development prospect forecast