当前位置:网站首页>[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
边栏推荐
- 程序設計訓練
- MySQL best practices for creating tables
- Pytorch introduction notes - use a simple example to observe the output size of each layer of forward propagation
- PHP processing JSON_ Decode() parses JSON stringify
- JSP syntax and JSTL tag
- [leetcode 67] sum of two binary numbers
- Doomsday (simple computational geometry)
- 线性代数第一章-行列式
- Addition, deletion, modification and query of MySQL table
- 治療TensorFlow後遺症——簡單例子記錄torch.utils.data.dataset.Dataset重寫時的圖片維度問題
猜你喜欢
Pytorch learning record (III): structure of neural network + using sequential and module to define the model
Gaussian processes of sklearn
線性代數第二章-矩陣及其運算
SQL injection
自动控制(韩敏版)
線性代數第一章-行列式
Delete and truncate
Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()
JDBC connection database
Automatic control (Han min version)
随机推荐
自动控制(韩敏版)
Algèbre linéaire chapitre 1 - déterminants
Generate excel template (drop-down selection, multi-level linkage)
PHP processing JSON_ Decode() parses JSON stringify
JDBC operation transaction
Contrôle automatique (version Han min)
線性代數第一章-行列式
The problem that the page will refresh automatically after clicking the submit button on the form is solved
Custom exception class
Three ways to create threads
Consistent hash algorithm used for redis cache load balancing
去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention
Viewer: introduce MySQL date function
Calculation (enter the calculation formula to get the result)
Collections multiple parameter sorting
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
Optional best practices
9.Life, the Universe, and Everything
MySQL best practices for creating tables
Addition, deletion, modification and query of MySQL table