当前位置:网站首页>15. Full arrangement
15. Full arrangement
2022-04-22 07:28:00 【yinhua405】
describe
Given a list of numbers , Return to all possible permutations .
You can assume that there are no duplicate numbers .
Examples
Examples 1:
Input :
list = [1]
Output :
[
[1]
]
Examples 2:
Input :
list = [1,2,3]
Output :
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
void backtrace(vector<vector<int>>&ret, vector<bool>&visited, stack<int>&stk, vector<int> &nums)
{
if (stk.size() == nums.size())
{
stack<int>tmpStk = stk;
vector<int> tmp(nums.size(), 0);
int i = nums.size() - 1;
while (!tmpStk.empty())
{
tmp[i] = tmpStk.top();
tmpStk.pop();
i--;
}
ret.push_back(tmp);
return;
}
for (int i = 0; i<nums.size(); i++)
{
if (visited[i] == true)
{
continue;
}
visited[i] = true;
stk.push(nums[i]);
backtrace(ret, visited, stk, nums);
visited[i] = false;
stk.pop();
}
}
vector<vector<int>> permute(vector<int> &nums) {
// write your code here
vector<vector<int>>ret;
vector<bool>visited(nums.size(), false);
stack<int>stk;
backtrace(ret, visited, stk, nums);
return ret;
}
版权声明
本文为[yinhua405]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220613324825.html
边栏推荐
- Codeforces Round #780 (Div. 3)
- 1242 · 无重叠区间
- The problem of interval summation -- difference
- [number theory] prime number (I): basic concepts, properties, conjectures and theorems
- L2-004 这是二叉搜索树吗?(先序输入&判断搜索二叉树&后序输出)
- P1095 [NOIP2007 普及组] 守望者的逃离
- synchronized锁优化的一些机制(锁升级)
- [number theory] prime number (III): prime number judgment method (simple method, modular 6 method, Rabin Miller and improvement)
- 332 · 恢复数组
- 详解树状数组模板——理论和代码的实现
猜你喜欢

小游戏——三子棋

SQL复习语法笔记整理,新鲜出炉

LeetCode - 4 - (接雨水、无重复字符的最长子串、分发糖果、二叉树的<前中后层>序遍历)

Interviewers often ask about the general process and special circumstances of object allocation

The thought and code of eight sorting

Cannot find interface mapping after updating HDF

我们常说的栈帧的内部结构是什么样的呢?

L1-064 估值一亿的AI核心代码 (20 分) 格式错误

278 · 绘制填充

LaTex用模板的时候图片的caption标题无法左对齐
随机推荐
队列(详解)——手撕队列习题
[number theory] prime number (III): prime number judgment method (simple method, modular 6 method, Rabin Miller and improvement)
手撕算法---LRU缓存淘汰策略,问的这么频繁
Interviewers often ask about the general process and special circumstances of object allocation
Byte Summer Internship - 20220304
(1) Download and installation of SQL Server
Yapi的安装与配置(转载)
If I make this silly mistake again/ (ㄒoㄒ)/~~
详解树状数组模板——理论和代码的实现
Codeforces Round #776 (Div. 3)
SQL review, grammar notes, fresh out
二叉树链式结构操作LeetCode+牛客(详解)
牛客xb月赛45题解
I.Jam-packed (均分/最大最小值) (2021年度训练联盟热身训练赛第五场)
[number theory] congruence (4): univariate linear congruence equations (elimination of two, Chinese remainder theorem)
Virtual machine disk space shrinks
单例池、单例Bean、单例模式
L2-001 紧急救援 (最短路Dijkstra的扩展 - 最短路径数&路径最大权值)
Leetcode - 8 - (sum of three numbers, zigzag transformation, sum of two numbers < linked list >, container with the most water, letter combination of telephone number)
Singleton pool, singleton bean, singleton mode