当前位置:网站首页>[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};
}
};
边栏推荐
- 【剑指 Offer】57. 和为s的两个数字
- Five message formats of OSPF
- Spend more time with your computer on this special holiday, HHH
- CVPR论文解读 | 弱监督的高保真服饰模特生成
- [shutter] shutter layout component (physicalmodel component)
- China's log saw blade market trend report, technological innovation and market forecast
- MySQL learning record (2)
- A river of spring water flows eastward
- Free open source web version of xshell [congratulations on a happy new year]
- 2019 Nanchang (relive the classic)
猜你喜欢

Image segmentation using pixellib

Gbase8s database type

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

Unexpectedly, there are such sand sculpture code comments! I laughed

Baidu sued a company called "Ciba screen"

Three chess games

MySQL learning record (8)

Basic knowledge of tree and binary tree (detailed illustration)

Etcd Raft 协议

*C language final course design * -- address book management system (complete project + source code + detailed notes)
随机推荐
Micro SD Card Industry Research Report - market status analysis and development prospect forecast
【剑指 Offer 】56 - II. 数组中数字出现的次数 II
MySQL learning record (5)
Construction and maintenance of business website [1]
Spend more time with your computer on this special holiday, HHH
Market trend report, technical innovation and market forecast of China's Micro pliers
Construction and maintenance of business website [5]
Chinese Indian seasoning market trend report, technical dynamic innovation and market forecast
MySQL learning record (1)
Baidu sued a company called "Ciba screen"
Download vagrant box file locally from Atlas and configuring it
Blue Bridge Cup Eliminate last one (bit operation, code completion)
[CV] Wu Enda machine learning course notes | Chapter 12
MySQL learning record (9)
Construction and maintenance of business websites [9]
Construction and maintenance of business websites [4]
Import a large amount of data to redis in shell mode
PIP audit: a powerful security vulnerability scanning tool
Physical layer cables and equipment
Report on investment development and strategic recommendations of China's vibration isolator market, 2022-2027