当前位置:网站首页>LeetCode 47. Full arrangement II
LeetCode 47. Full arrangement II
2022-07-02 06:10:00 【Great white sheep_ Aries】
Title Description
solution
This problem is also the problem of backtracking pruning , But there are some skills in pruning here . Let's analyze first , for example , n u m s = [ 1 , 2 , 2 ′ ] nums = [1,2,2'] nums=[1,2,2′], Without pruning , We will get the following results
[
[1,2,2'],[1,2',2],
[2,1,2'],[2,2',1],
[2',1,2],[2',2,1]
]
If we ensure that the relative positions of the same elements in the arrangement remain unchanged , for instance n u m s = [ 1 , 2 , 2 ′ ] nums = [1,2,2'] nums=[1,2,2′] This example , I keep in line 2 2 2 Has been 2 ′ 2' 2′ front . In this case , You from above 6 You can only pick out 3 Two permutations meet this condition :
[
[1,2,2'],[2,1,2'],[2,2',1]
]
Think about , It should be easy to understand the principle :
The reason why the standard full permutation algorithm repeats , Because the arrangement sequence formed by the same elements is regarded as different sequences , But actually they should be the same ; And if you fix the sequence order formed by the same elements , Of course, it avoids repetition
Then reflect it on the code , Pay attention to the pruning logic :
// Newly added pruning logic , Fix the relative position of the same element in the arrangement
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
// If the previous adjacent equal elements have not been used , Then skip
continue;
}
// choice nums[i]
The complete implementation is as follows
class Solution {
public:
vector<vector<int>> res;
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<int> track;
vector<int> used(nums.size(), 0);
sort(nums.begin(), nums.end());
backtrace(nums, track, used);
return res;
}
void backtrace(vector<int>& nums, vector<int>& track, vector<int>& used)
{
if (track.size() == nums.size())
{
res.push_back(track);
return;
}
for (int i = 0; i < nums.size(); i++)
{
if (used[i]) continue;
if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == 0) continue;
track.push_back(nums[i]);
used[i] = 1;
backtrace(nums, track, used);
used[i] = 0;
track.pop_back();
}
}
};
边栏推荐
- Current situation analysis of Devops and noops
- Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
- Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
- Ros2 --- lifecycle node summary
- 492. Construction rectangle
- Shenji Bailian 3.52-prim
- I/o impressions from readers | prize collection winners list
- CNN可视化技术 -- CAM & Grad-CAM详解及pytorch简洁实现
- From design delivery to development, easy and efficient!
- AttributeError: ‘str‘ object has no attribute ‘decode‘
猜你喜欢

Can't the dist packaged by vite be opened directly in the browser

社区说|Kotlin Flow 的原理与设计哲学

Eco express micro engine system has supported one click deployment to cloud hosting

The official zero foundation introduction jetpack compose Chinese course is coming!

Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!

深度学习分类网络--VGGNet

Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)

Ros2 --- lifecycle node summary

Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music

Detailed notes of ES6
随机推荐
Go 学习笔记整合
Google play academy team PK competition, official start!
数据回放伴侣Rviz+plotjuggler
TI毫米波雷达学习(一)
I/o multiplexing & event driven yyds dry inventory
Reading classic literature -- Suma++
Classic literature reading -- deformable Detr
社区说|Kotlin Flow 的原理与设计哲学
Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
Redis Key-Value数据库【初级】
BGP报文详细解释
Shenji Bailian 3.53-kruskal
格式校验js
Data playback partner rviz+plotjuggler
Stc8h8k series assembly and C51 actual combat - keys allow key counting (using falling edge interrupt control)
JS determines whether the mobile terminal or the PC terminal
Redis Key-Value数据库 【高级】
Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
LeetCode 90. 子集 II
Keepalived installation, use and quick start