当前位置:网站首页>[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;
}
};
边栏推荐
- How to prevent your jar from being decompiled?
- [use of pointer and pointer and array]
- Construction and maintenance of business websites [6]
- 【剑指 Offer】57. 和为s的两个数字
- How does esrally perform simple custom performance tests?
- SQL必需掌握的100个重要知识点:使用游标
- Construction and maintenance of business websites [7]
- Hot backup routing protocol (HSRP)
- MySQL learning record (4)
- Redis distributed lock failure, I can't help but want to burst
猜你喜欢

CVPR论文解读 | 弱监督的高保真服饰模特生成

MySQL learning record (2)

Research Report on ranking analysis and investment strategic planning of RFID market competitiveness of China's industrial manufacturing 2022-2028 Edition

Analysis of neural network

MySQL learning record (3)
![[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)
![[shutter] shutter page Jump (route | navigator | page close)](/img/af/3fb2ca18bcec23a5c0c6897570fb53.gif)
[shutter] shutter page Jump (route | navigator | page close)

D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)

Technical solution of vision and manipulator calibration system

Cardinality sorting (detailed illustration)
随机推荐
[use of pointer and pointer and array]
pyqt圖片解碼 編碼後加載圖片
Physical layer cables and equipment
B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
[Yu Yue education] reference materials of analog electronic technology of Nanjing Institute of information technology
Research Report on crude oil tanker industry - market status analysis and development prospect forecast
加了定位的文字如何水平垂直居中
Technical solution of vision and manipulator calibration system
Image segmentation using pixellib
Detailed explanation of OSI seven layer model
Etcd Raft 协议
Research Report on market supply and demand and strategy of China's right-hand outward rotation entry door industry
D4: unpaired image defogging, self enhancement method based on density and depth decomposition (CVPR 2022)
Plastic granule Industry Research Report - market status analysis and development prospect forecast
Cloud computing technology [1]
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 [17]
Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
MySQL learning record (2)