当前位置:网站首页>[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};
}
};
边栏推荐
- Analysis of neural network
- Research Report on minimally invasive medical robot industry - market status analysis and development prospect prediction
- 【剑指 Offer】57. 和为s的两个数字
- Technical solution of vision and manipulator calibration system
- Construction and maintenance of business website [2]
- Research Report on market supply and demand and strategy of China's plastic trunking industry
- PIP version update timeout - download using domestic image
- Accounting regulations and professional ethics [16]
- 发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...
- MySQL learning record (7)
猜你喜欢

VictoriaMetrics 简介

How to test the process of restoring backup files?

Redis分布式锁故障,我忍不住想爆粗...

Browser - clean up the cache of JS in the page

Gbase8s database type

Error in PIP installation WHL file: error: is not a supported wheel on this platform

One week dynamics of dragon lizard community | 2.07-2.13

qwb2018_ core kernel_ rop

pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform

发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...
随机推荐
[C language] [sword finger offer article] - replace spaces
In depth research and investment feasibility report on the global and China active vibration isolation market 2022-2028
MySQL learning record (6)
2019 Nanchang (relive the classic)
Accounting regulations and professional ethics [16]
Redis distributed lock failure, I can't help but want to burst
地理探测器原理介绍
Market trend report, technical innovation and market forecast of China's Micro pliers
Basic knowledge of tree and binary tree (detailed illustration)
Accounting regulations and professional ethics [19]
[shutter] shutter layout component (opacity component | clipprect component | padding component)
Construction and maintenance of business websites [6]
Detailed explanation of OSI seven layer model
Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
In depth research and investment feasibility report of global and Chinese isolator industry, 2022-2028
Research Report on crude oil tanker industry - market status analysis and development prospect forecast
beginning
Blue Bridge Cup Eliminate last one (bit operation, code completion)
MySQL learning record (5)
MySQL learning record (9)