当前位置:网站首页>Leetcode47. full arrangement II
Leetcode47. full arrangement II
2022-06-30 08:05:00 【Lafiteeee】
Title Description :
Given a sequence that can contain repeating numbers nums , In any order Returns all non repeating permutations .
Ideas
The problem of permutation is to use backtracking .
because nums There may be the same number in the array , So there will be repeated permutations in the full permutation . This requires pruning . Ideas as follows :
First of all, will nums Array to sort , Keep the same number next to ;
Then, in the process of recursively filling in numbers, you need to traverse every time vis Array to find unused numbers , So we only look for the first of the unused numbers , for example nums = [1, 1, 2], When the first one 1 After all the initial permutations have been found , Start looking for the second 1 The arrangement at the beginning , At this moment vis = [0, 0, 0], Indicates that none of the three numbers are used , But for the first 2 individual 1 Come on , The previous number is the same as it but not used , Explain that this situation has occurred before , Can directly continue skip . The code for pruning is :
if (i > 0 && nums[i] == nums[i - 1] && !vis[i - 1]) {
// prune , Avoid filling in numbers that have been filled in this position
continue;
}
Answer key :
class Solution {
vector<bool> vis;
public:
void backtrace(int idx, vector<int>& tmp, vector<vector<int>>& ans, vector<int>& nums) {
if (idx == nums.size()) {
//idx And size equal , Explain that you have filled in all the numbers , At this time tmp What is stored is a
// Legal answer , Add it to ans Array , And back to .
ans.emplace_back(tmp);
return;
}
for (int i = 0; i < nums.size(); i++) {
if (i > 0 && nums[i] == nums[i - 1] && !vis[i - 1]) {
// prune , Avoid filling in numbers that have been filled in this position
continue;
}
if (!vis[i]) {
tmp.emplace_back(nums[i]);
vis[i] = 1;
backtrace(idx + 1, tmp, ans, nums);
vis[i] = 0;
tmp.pop_back();
}
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
vis.resize(nums.size());
sort(nums.begin(), nums.end());
vector<vector<int>> ans;
vector<int> tmp;
backtrace(0, tmp, ans, nums);
return ans;
}
};
边栏推荐
- 2021-10-27 [WGS] pacbio third generation methylation modification process
- February 14, 2022 [reading notes] - life science based on deep learning Chapter 2 Introduction to deep learning (Part 1)
- Deep learning - brnn and DRNN
- Combinatorial mathematics Chapter 1 Notes
- Construction of module 5 of actual combat Battalion
- Efga design open source framework fabulous series (I) establishment of development environment
- Deep learning - goal orientation
- Construction of energy conservation supervision system for campus buildings of ankery University
- 2021-10-29 [microbiology] qiime2 sample pretreatment form automation script
- Examen final - notes d'apprentissage PHP 5 - Tableau PHP
猜你喜欢

深度学习——残差网络ResNets

【Tensorflow-gpu】window11下深度学习环境搭建

Deep learning -- using word embedding and word embedding features

深度学习——GRU单元

回文子串、回文子序列
![February 14, 2022 [reading notes] - life science based on deep learning Chapter 2 Introduction to deep learning (Part 1)](/img/ff/e4df5a66cda74ee0d71015b7d1a462.jpg)
February 14, 2022 [reading notes] - life science based on deep learning Chapter 2 Introduction to deep learning (Part 1)

Deep learning - networks in networks and 1x1 convolution

Introduction notes to pytorch deep learning (10) neural network convolution layer

小程序使用二维码插件

期末复习-PHP学习笔记1
随机推荐
1163 Dijkstra Sequence
想转行,却又不知道干什么?此文写给正在迷茫的你
Deep learning - networks in networks and 1x1 convolution
Deep learning vocabulary representation
Construction of energy conservation supervision system for campus buildings of ankery University
Hit the industry directly | the flying propeller launched the industry's first model selection tool
[flower carving experience] 12 build the Arduino development environment of esp32c3
Deep learning - LSTM
Wechat applet reports errors using vant web app
深度学习——序列模型and数学符号
鲸探NFT数字臧品系统开发技术分享
Introduction to opencv (I): image reading and display
Deep learning - brnn and DRNN
Combinatorial mathematics Chapter 2 Notes
安科瑞高等学校校园建筑节能监管系统建设
【花雕体验】13 搭建ESP32C3之PlatformIO IDE开发环境
More, faster, better and cheaper. Here comes the fastdeploy beta of the low threshold AI deployment tool!
2021.11.20 [reading notes] | differential variable splicing events and DTU analysis
【JUC系列】Fork/Join框架之概览
2021-10-27 [WGS] pacbio third generation methylation modification process