当前位置:网站首页>[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;
}
};
边栏推荐
- pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
- What is the difference between programming in real work and that in school?
- SQL必需掌握的100个重要知识点:使用游标
- PIP audit: a powerful security vulnerability scanning tool
- 加了定位的文字如何水平垂直居中
- Construction and maintenance of business website [5]
- Plastic floating dock Industry Research Report - market status analysis and development prospect forecast
- MySQL learning record (5)
- Baidu sued a company called "Ciba screen"
- treevalue——Master Nested Data Like Tensor
猜你喜欢
[use of pointer and pointer and array]
Error in PIP installation WHL file: error: is not a supported wheel on this platform
Five message formats of OSPF
VictoriaMetrics 简介
Structure array, pointer and function and application cases
GEE:(二)对影像进行重采样
[shutter] shutter layout component (Introduction to layout component | row component | column component | sizedbox component | clipoval component)
Three chess games
如何访问kubernetes API?
The neo4j skill tree was officially released to help you easily master the neo4j map database
随机推荐
Research Report on minimally invasive medical robot industry - market status analysis and development prospect prediction
发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...
Free open source web version of xshell [congratulations on a happy new year]
Construction and maintenance of business website [5]
Gbase 8s database basic syntax
Research Report on micro gripper industry - market status analysis and development prospect prediction
tinymce可视化编辑器增加百度地图插件
Analysis of enterprise financial statements [1]
Research Report on market supply and demand and strategy of China's right-hand outward rotation entry door industry
MySQL learning record (3)
[shutter] statefulwidget component (create statefulwidget component | materialapp component | scaffold component)
Go web programming practice (2) -- process control statement
Plastic floating dock Industry Research Report - market status analysis and development prospect forecast
[CV] Wu Enda machine learning course notes | Chapter 12
Construction and maintenance of business websites [6]
Research Report on market supply and demand and strategy of China's plastic pump industry
VIM command-t plugin error: unable to load the C extension - VIM command-t plugin error: could not load the C extension
Accounting regulations and professional ethics [16]
pyqt圖片解碼 編碼後加載圖片
China's log saw blade market trend report, technological innovation and market forecast