当前位置:网站首页>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;
}
};
边栏推荐
- November 22, 2021 [reading notes] - bioinformatics and functional genomics (Chapter 5, section 4, hidden Markov model)
- Experiment 4 QT
- 亚马逊测评术语有哪些?
- 2021-10-27 [WGS] pacbio third generation methylation modification process
- Halcon12+vs2013 C # configuration
- Final review -php learning notes 1
- Examen final - notes d'apprentissage PHP 5 - Tableau PHP
- November 21, 2021 [reading notes] - bioinformatics and functional genomics (Chapter 5 advanced database search)
- tp5设置直接下载文件
- 2021-10-29 [microbiology] qiime2 sample pretreatment form automation script
猜你喜欢
![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)

Use of nested loops and output instances

Development technology sharing of Jingtan NFT digital collection system
![2021.11.20 [reading notes] | differential variable splicing events and DTU analysis](/img/02/6971454e51c015990b5b60b357ee1d.jpg)
2021.11.20 [reading notes] | differential variable splicing events and DTU analysis

鲸探NFT数字臧品系统开发技术分享

Halcon12+vs2013 C # configuration

跳槽字节跳动很难嘛?掌握这些技巧,你也能轻松通过

Deep learning - LSTM

【JUC系列】Fork/Join框架之概览

为什么大学毕业了还不知道干什么?
随机推荐
多快好省,低门槛AI部署工具FastDeploy测试版来了!
2022.01.20 [bug note] | qiime2: an error was encoded while running dada2 in R (return code 1)
C. Fishingprince Plays With Array
Deep learning -- feature point detection and target detection
Acreems energy efficiency management platform escorts the power safety of high-rise residential areas
February 14, 2022 [reading notes] - life science based on deep learning Chapter 2 Introduction to deep learning (Part 1)
Final review -php learning notes 2-php language foundation
Introduction notes to pytorch deep learning (10) neural network convolution layer
What management improvements can CRM bring to enterprises
Deep learning - brnn and DRNN
【JUC系列】Fork/Join框架之概览
[flower carving experience] 13 build the platformio ide development environment of esp32c3
深度学习——LSTM
【花雕体验】14 行空板pinpong库测试外接传感器模块(之一)
深度学习——Bounding Box预测
How to handle the expired data of redis and what are the elimination mechanisms?
Armv8 (coretex-a53) debugging based on openocd and ft2232h
深度学习——使用词嵌入and词嵌入特征
JS code case
December 4, 2021 [metagenome] - sorting out the progress of metagenome process construction