当前位置:网站首页>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();
}
}
};
边栏推荐
- LeetCode 83. 删除排序链表中的重复元素
- Problems encountered in uni app development (continuous update)
- Deep learning classification network -- alexnet
- Data playback partner rviz+plotjuggler
- Use some common functions of hbuilderx
- Summary of MySQL constraints
- External interrupts cannot be accessed. Just delete the code and restore it Record this unexpected bug
- 495.提莫攻击
- 官方零基础入门 Jetpack Compose 的中文课程来啦!
- Some experience of exercise and fitness
猜你喜欢
Lucene Basics
浏览器原理思维导图
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
经典文献阅读之--Deformable DETR
Eco express micro engine system has supported one click deployment to cloud hosting
网络相关知识(硬件工程师)
Can't the dist packaged by vite be opened directly in the browser
CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
ESP8266与STC8H8K单片机联动——天气时钟
Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
随机推荐
Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
WLAN相关知识点总结
Redis Key-Value数据库 【秒杀】
Deep learning classification network -- Network in network
Lucene Basics
Zhuanzhuanben - LAN construction - Notes
深度学习分类网络--Network in Network
Classic literature reading -- deformable Detr
The real definition of open source software
浏览器原理思维导图
LeetCode 283. 移动零
keepalived安装使用与快速入门
Shenji Bailian 3.53-kruskal
使用sha256文件验证下载的文件
Style modification of Mui bottom navigation
ROS create workspace
CNN可视化技术 -- CAM & Grad-CAM详解及pytorch简洁实现
Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice
token过期自动续费方案和实现