当前位置:网站首页>[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 enterprise financial statements [2]
- Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
- MySQL learning record (8)
- B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
- Hot backup routing protocol (HSRP)
- Gbase 8s database basic syntax
- MySQL learning record (3)
- Structure array, pointer and function and application cases
- Research Report on market supply and demand and strategy of China's plastic pump industry
- Internet Explorer ignores cookies on some domains (cannot read or set cookies)
猜你喜欢

treevalue——Master Nested Data Like Tensor

Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source

qwb2018_ core kernel_ rop

MySQL learning record (3)

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

Etcd Raft 协议

Baidu sued a company called "Ciba screen"

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

PIP audit: a powerful security vulnerability scanning tool

Huawei Hongmeng watch achieves fireworks display effect on New Year's Eve
随机推荐
Blue Bridge Cup Eliminate last one (bit operation, code completion)
Plastic floating dock Industry Research Report - market status analysis and development prospect forecast
Go language learning summary (5) -- Summary of go learning notes
PIP version update timeout - download using domestic image
Import a large amount of data to redis in shell mode
Today, I met a Alipay and took out 35K. It's really sandpaper to wipe my ass. it's a show for me
Plastic granule Industry Research Report - market status analysis and development prospect forecast
Get weekday / day of week for datetime column of dataframe - get weekday / day of week for datetime column of dataframe
Hot backup routing protocol (HSRP)
Download vagrant box file locally from Atlas and configuring it
Research Report on market supply and demand and strategy of Chinese garden equipment industry
[shutter] shutter page Jump (route | navigator | page close)
China plastic box market trend report, technological innovation and market forecast
[shutter] statefulwidget component (pageview component)
MySQL learning notes (Advanced)
One week dynamics of dragon lizard community | 2.07-2.13
Research Report on crude oil tanker industry - market status analysis and development prospect forecast
treevalue——Master Nested Data Like Tensor
MySQL inserts Chinese data and reports an error. Set the default collation
China Indonesia advanced wound care market trend report, technological innovation and market forecast