当前位置:网站首页>LeetCode 47. 全排列 II
LeetCode 47. 全排列 II
2022-07-02 06:07:00 【大白羊_Aries】
题目描述
解法
这道题同样还是回溯剪枝的问题,但是这里的剪枝有点技巧。我们先来分析一下,例如, n u m s = [ 1 , 2 , 2 ′ ] nums = [1,2,2'] nums=[1,2,2′],不剪枝的话,会得到如下的结果
[
[1,2,2'],[1,2',2],
[2,1,2'],[2,2',1],
[2',1,2],[2',2,1]
]
如果我们保证相同元素在排列中的相对位置保持不变,比如说 n u m s = [ 1 , 2 , 2 ′ ] nums = [1,2,2'] nums=[1,2,2′] 这个例子,我保持排列中 2 2 2 一直在 2 ′ 2' 2′ 前面。这样的话,你从上面 6 个排列中只能挑出 3 个排列符合这个条件:
[
[1,2,2'],[2,1,2'],[2,2',1]
]
仔细思考,应该很容易明白其中的原理:
标准全排列算法之所以出现重复,是因为把相同元素形成的排列序列视为不同的序列,但实际上它们应该是相同的;而如果固定相同元素形成的序列顺序,当然就避免了重复
那么反映到代码上,你注意看这个剪枝逻辑:
// 新添加的剪枝逻辑,固定相同的元素在排列中的相对位置
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
// 如果前面的相邻相等元素没有用过,则跳过
continue;
}
// 选择 nums[i]
完整实现如下
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();
}
}
};
边栏推荐
- Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
- Mathematical statistics and machine learning
- 495.提莫攻击
- Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
- Redis key value database [advanced]
- Problems encountered in uni app development (continuous update)
- 492. Construction rectangle
- 神机百炼3.53-Kruskal
- 使用sha256文件验证下载的文件
- Web页面用户分步操作引导插件driver.js
猜你喜欢
Reading notes of cgnf: conditional graph neural fields
Summary of MySQL constraints
Software testing Q & A
[whether PHP has soap extensions installed] a common problem for PHP to implement soap proxy: how to handle class' SoapClient 'not found in PHP
How vite is compatible with lower version browsers
社区说|Kotlin Flow 的原理与设计哲学
Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
Shenji Bailian 3.52-prim
Frequently asked questions about jetpack compose and material you
步骤详解 | 助您轻松提交 Google Play 数据安全表单
随机推荐
Flutter 混合开发: 开发一个简单的快速启动框架 | 开发者说·DTalk
Error creating bean with name 'instanceoperatorclientimpl' defined in URL when Nacos starts
On Web server
如何使用MITMPROXy
Regular expression summary
Reading classic literature -- Suma++
谷歌出海创业加速器报名倒计时 3 天,创业人闯关指南提前收藏!
Stick to the big screen UI, finereport development diary
Classic literature reading -- deformable Detr
深度学习分类网络 -- AlexNet
Deep learning classification network -- alexnet
Redis Key-Value数据库 【秒杀】
复杂 json数据 js前台解析 详细步骤《案例:一》
Deep learning classification network -- Network in network
Stc8h8k series assembly and C51 actual combat - digital display ADC, key serial port reply key number and ADC value
Spark overview
495.提莫攻击
Software testing Q & A
Ti millimeter wave radar learning (I)
PHP development and testing WebService (soap) -win