当前位置:网站首页>[sword finger offer] 56 - I. the number of numbers in the array
[sword finger offer] 56 - I. the number of numbers in the array
2022-07-02 21:43:00 【LuZhouShiLi】
The finger of the sword Offer 56 - I. The number of occurrences of numbers in an array
subject
An integer array nums Except for two numbers , The other numbers appear twice . Please write a program to find out these two numbers that only appear once . The required time complexity is O(n), The space complexity is O(1).
Ideas
An array nums Do it once or calculate , Get two or operation results that only appear once , Initialize a number m = 1,x and y Is different , So there must be one 1, Then just find it as 1 Binary bit of , It can be nums Split into two sub arrays , Initialize an auxiliary variable m=1 , Judge from right to left through the and operation , Get integer x⊕y Who is it 1. And then split nums For two subarrays , Traverse two sub arrays to execute or , You can get two numbers that only appear once .
Code
class Solution {
public:
vector<int> singleNumbers(vector<int>& nums) {
int x = 0, y = 0, n = 0, m = 1;
for(int num : nums)
n ^= num;
while((n & m) == 0)
m <<= 1;
for(int num : nums) {
if(num & m) x ^= num;
else y ^= num;
}
return vector<int> {
x, y};
}
};
边栏推荐
- [use of pointer and pointer and array]
- qwb2018_ core kernel_ rop
- Common authority query instructions in Oracle
- 如何防止你的 jar 被反编译?
- A river of spring water flows eastward
- VIM command-t plugin error: unable to load the C extension - VIM command-t plugin error: could not load the C extension
- Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
- Basic knowledge of tree and binary tree (detailed illustration)
- Chargement de l'image pyqt après décodage et codage de l'image
- pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
猜你喜欢
Unexpectedly, there are such sand sculpture code comments! I laughed
qwb2018_ core kernel_ rop
It is said that this year gold three silver four has become gold one silver two..
MySQL learning record (6)
[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)
PIP audit: a powerful security vulnerability scanning tool
kernel tty_ struct
Gbase8s database type
Report on investment development and strategic recommendations of China's vibration isolator market, 2022-2027
MySQL learning record (8)
随机推荐
MySQL learning record (7)
Get weekday / day of week for datetime column of dataframe - get weekday / day of week for datetime column of dataframe
Construction and maintenance of business website [5]
It is said that this year gold three silver four has become gold one silver two..
[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)
Construction and maintenance of business websites [7]
Summary of the first week of summer vacation
3DES (deSede) encryption CBC mode pkcs7padding filling Base64 encoding key 24byte iv8byte
地理探测器原理介绍
Accounting regulations and professional ethics [18]
Spend more time with your computer on this special holiday, HHH
D4: unpaired image defogging, self enhancement method based on density and depth decomposition (CVPR 2022)
*C language final course design * -- address book management system (complete project + source code + detailed notes)
发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...
MySQL learning record (8)
关于测试用例
Technical solution of vision and manipulator calibration system
Gbase 8s database basic syntax
pyqt图片解码 编码后加载图片
Capacity expansion mechanism of ArrayList