当前位置:网站首页>[leetcode 954] double pair array
[leetcode 954] double pair array
2022-04-23 06:23:00 【Don't steal my energy】
Title Description
Given an array of integers of even length arr, Only for arr After restructuring, we can meet “ For each 0 <= i < len(arr) / 2, There are arr[2 * i + 1] = 2 * arr[2 * i]” when , return true; otherwise , return false.
Example 1
Input :arr = [3,1,3,6]
Output :false
Example 2
Input :arr = [2,1,2,6]
Output :false
Example 3
Input :arr = [4,-2,2,-4]
Output :true
explain : It can be used [-2,-4] and [2,4] These two groups are made up of [-2,-4,2,4] or [2,4,-2,-4]
- 0 <= arr.length <= 3 * 104
- arr.length It's even
- -105 <= arr[i] <= 105
Knowledge points involved : Hashtable greedy Array Sort
Ideas
It's actually a matching problem , First, the elements in the vector and the number of times they appear are stored in the hash table map in , Sort the elements in the vector and then traverse , Judge each number ( Even number ) Whether it can be successfully combined with twice its number or half its number , Current number is less than 0 The explanation is not enough , It can't be combined ,( Odd numbers only need to be judged Whether this number and its double number can be successfully combined ); Finally, traverse it again , Check all in the hash table key Corresponding value ( The number of times the corresponding element appears ) Is it all for 0, Once not for 0 Express , Combination failed , return false.
bool canReorderDoubled(vector<int>& arr) {
unordered_map<int,int> count;
for(int x:arr)
count[x]++;
sort(arr.begin(),arr.end());// Sort Avoid excessive span , Cause confusion and error in matching
for(int i=0;i<arr.size();i++){
if(arr[i]%2==0 && count[arr[i]] && count[arr[i]/2])// Even number , Judge whether half of the number can be combined with him
{
count[arr[i]]--;
count[arr[i]/2]--;
}
else if(arr[i]%2==0 && count[arr[i]] && count[2*arr[i]])// Even number , Judge the number of 2 Whether the number of times can be combined with him
{
count[arr[i]]--;
count[2*arr[i]]--;
}
else if(arr[i]%2==1 && count[arr[i]] && count[2*arr[i]])// Odd hour , Judge the number of 2 Whether the number of times can be combined with him
{
count[arr[i]]--;
count[2*arr[i]]--;
}
}
for(int x:arr)
if(count[x]>0)// Indicates that the combination failed
return 0;
return 1;
}
版权声明
本文为[Don't steal my energy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617011971.html
边栏推荐
- In depth understanding of the relationship between dncblevel and noise denoising in the paper
- 11.a==b?
- Anaconda installed pyqt5 and pyqt5 tools without designer Exe problem solving
- Use of multithreaded executors
- String notes
- 自动控制原理知识点整合归纳(韩敏版)
- MySQL occasional Caton
- Remedy after postfix becomes a spam transit station
- What is the difference between the basic feasible solution and the basic feasible solution in linear programming?
- RPC must know and know
猜你喜欢

線性代數第二章-矩陣及其運算

自动控制原理知识点整合归纳(韩敏版)

C language file operation

Pyqy5 learning (2): qmainwindow + QWidget + qlabel

Pyqt5 learning (I): Layout Management + signal and slot association + menu bar and toolbar + packaging resource package

Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison

lambda expressions

On traversal of binary tree

Contrôle automatique (version Han min)

Pytorch learning record (III): structure of neural network + using sequential and module to define the model
随机推荐
What is the difference between the basic feasible solution and the basic feasible solution in linear programming?
Create binary tree
治療TensorFlow後遺症——簡單例子記錄torch.utils.data.dataset.Dataset重寫時的圖片維度問題
Algèbre linéaire chapitre 1 - déterminants
Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention
自動控制(韓敏版)
Implementation of displaying database pictures to browser tables based on thymeleaf
Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison
Advanced operation of idea debug
Mysql database foundation
Framework analysis 2 Source code - login authentication
JDBC operation transaction
How to grow at work
Viewer: introduce MySQL date function
How does MySQL convert stored seconds into dates
Protected (members modified by protected are visible to this package and its subclasses)
2. Devops sonar installation
@Problems caused by internal dead loop of postconstruct method
Kalman filter and inertial integrated navigation
2. Average length of words