当前位置:网站首页>Niuke: two numbers that only appear once in the array
Niuke: two numbers that only appear once in the array
2022-06-11 02:50:00 【lsgoose】


Catalog
Method 2 : Exclusive or operation
Method 1 : Hashtable
Set up a hash table to store < Numbers , Number of occurrences > Hash table for , Search times and sorting after
class Solution {
public:
/**
* The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
*
*
* @param array int integer vector
* @return int integer vector
*/
vector<int> FindNumsAppearOnce(vector<int>& array) {
// write code here
unordered_map<int, int> mp;
vector<int> res;
for(int i=0;i<array.size();++i){
mp[array[i]]++;
}
for(int i=0;i<array.size();++i){
if(mp[array[i]]==1){
res.push_back(array[i]);
}
}
if(res[0]>res[1]){
swap(res[0], res[1]);
}
return res;
}
};Method 2 : Exclusive or operation
It's a very clever idea :
Let's first find the XOR of all numbers , This will result in the XOR of two answer numbers . Then we find the first one for this result 1.
This position is very clever , The two answer numbers are different in this position , Other numbers will appear twice in this position , And then do XOR operation to offset .
The code is as follows :
class Solution {
public:
/**
* The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
*
*
* @param array int integer vector
* @return int integer vector
*/
vector<int> FindNumsAppearOnce(vector<int>& array) {
// write code here
vector<int> res(2,0);
int temp=0;
for(int i=0;i<array.size();++i){
temp^=array[i];
}
int k=1;
while((k&temp)==0){
k<<=1;
}
for(int i=0;i<array.size();++i){
if((k&array[i])==0){
res[0]^=array[i];
}else{
res[1]^=array[i];
}
}
if(res[0]>res[1]){
swap(res[0], res[1]);
}
return res;
}
};边栏推荐
- How to fix syntax errors in WordPress websites
- WordPress article directory plug-in luckywp table of contents setup tutorial
- 【大咖秀】博睿数据眼中的AIOps,选择正确的赛道正确的人
- What is ttfb
- Write my Ini configuration file error
- 剑指 Offer II 079. 所有子集
- jdbc工具类的问题
- Unity HTC and Pico are the same
- Istio installation and use
- CPT 102_LEC 13-14
猜你喜欢

逃离大城市的年轻人:扛住了房价和压力,没扛住流行病

Istio安装与使用

How to use phpMyAdmin to optimize MySQL database

Looking at the ups and downs of the mobile phone accessories market from the green Union's sprint for IPO

MOFs, metal organic framework materials of folic acid ligands, are loaded with small molecule drugs such as 5-fluorouracil, sidabelamine, taxol, doxorubicin, daunorubicin, ibuprofen, camptothecin, cur

Unity animator rewind

Write my Ini configuration file error

Project load failed

One line of code solves the problem that the time to fetch datetime from MySQL database is less than eight hours

How to add two factor authentication for WordPress websites
随机推荐
Prophet
蓝桥杯_小蓝吃糖果_鸽巢原理 / 抽屉原理
mysql重装时写my.ini配置文件出错
2022年熔化焊接与热切割操作证考试题库及答案
Live broadcast! Feature matching of orb-slam3 series (mlpnp, word bag model, etc.).
怎样简洁明了地说清楚产品需求?
Jetpack compose box control
Databinding escaping with presentation symbols
error exepected identifier before ‘(‘ token, grpc 枚举类编译错误
Forest v1.5.22 发布!支持Kotlin
MOFs, metal organic framework materials of folic acid ligands, are loaded with small molecule drugs such as 5-fluorouracil, sidabelamine, taxol, doxorubicin, daunorubicin, ibuprofen, camptothecin, cur
Uni app - one click access to user information
WordPress upgrade error: briefly unavailable for scheduled maintenance [resolved]
A digit DP
When the interviewer opens his mouth, he comes to compose. Is this the case now?
Why did those who left Beijing, Shanghai and Guangzhou with a smile cry in the end?
Les produits financiers de l'assurance - rente peuvent - ils être composés? Quel est le taux d'intérêt?
20220610 星期五
How to state clearly and concisely the product requirements?
判断一串数字是否是快速排序某一次的结果