【LeetCode】Permutations(全排列)
这道题是LeetCode里的第46道题。
题目要求:
给定一个没有重复数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
回溯法解题,函数的递归调用。
提交代码:
class Solution {
public:
void getsol(vector<int>& nums, int a[], int k, vector<int>& sol, vector<vector<int>>& res) {
for (int i = 0; i < nums.size(); i++) {
if (a[i]) {
a[i] = 0;
sol.push_back(nums[i]);
getsol(nums, a, k + 1, sol, res);
sol.pop_back();
a[i] = 1;
}
if (k == nums.size()) {
res.push_back(sol);
return;
}
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>>res;
vector<int>sol;
int *a = new int[nums.size()];//这个数组用来记录该数组是否在sol中,避免重复
for (int i = 0; i < nums.size(); i++)a[i] = 1;
for (int i = 0; i < nums.size(); i++) {
a[i] = 0;
sol.push_back(nums[i]);
getsol(nums, a, 1, sol, res);
sol.pop_back();
a[i] = 1;
}
return res;
}
};
提交结果:

个人总结:
回溯法的题目还是需要多多调试熟悉一下它的运行过程,堆栈的调用和转移。
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
if(nums.empty())return vector<vector<int>>(1,vector<int>());
vector<vector<int>>res;
int first=nums[0];
nums.erase(nums.begin());
vector<vector<int>>words=permute(nums);
for(auto &a:words) {
for(int i=0;i<=a.size();++i) {
a.insert(a.begin()+i,first);
res.push_back(a);
a.erase(a.begin()+i);
}
}
return res;
}
};

像这种方法是在把从下一层返回的 res 赋予 words,然后使用 a 容器遍历 words,把 get 到的数 first 依次插入到容器 a 里在放入 res 中保存在把插入的 first 取出,然后在把所得的结果一步一步以 res 的形式返回给上一层。
【LeetCode】Permutations(全排列)的更多相关文章
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- [CareerCup] 9.5 Permutations 全排列
9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...
- 每日一题-——LeetCode(46)全排列
题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...
- LeetCode:全排列II【47】
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
- LeetCode:全排列【46】
LeetCode:全排列[46] 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2 ...
- LeetCode 47——全排列 II
1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- [leetcode]46. Permutations全排列(给定序列无重复元素)
Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...
随机推荐
- AJPFX分享java排序之希尔排序
(1)基本思想:算法先将要排序的一组数按某个增量d(n/2,n为要排序数的个数)分成若干组,每组中记录的下标相差d.对每组中全部元素进行直接插入排序,然后再用一个较小的增量(d/2)对它进行分组,在每 ...
- bsub && lsf 介绍
文章转载地址:http://www.bbioo.com/lifesciences/40-114265-1.html LSF系统介绍 http://scc.ustc.edu.cn/zh_CN/ 中科大超 ...
- gvim -- 跳转命令,查找格式,正则
1.跳转命令 ‘w'单词前进,'b'单词后退,'e'单词前进,‘ge’单词后退,存在单词词首词尾区别,'W''B''E''gE'将不以单词区分,以空格区分 ‘$’行尾,'^'非空白行首,'0'行首 ‘ ...
- codevs 2277 爱吃皮蛋的小明(水题日常)
时间限制: 1 s 空间限制: 32000 KB 题目等级 : 白银 Silver 题目描述 Description 小明特别爱吃蛋,特别是皮蛋.他一次可以吃一个蛋或者两个蛋(整个吞下去),而且他 ...
- 一个具体的例子学习Java volatile关键字
相信大多数Java程序员都学习过volatile这个关键字的用法.百度百科上对volatile的定义: volatile是一个类型修饰符(type specifier),被设计用来修饰被不同线程访问和 ...
- SAP产品的Field Extensibility
SAP开发人员的工作职责,除了实现软件的功能性需求外,还会花费相当的精力实现一些非功能性需求,来满足所谓的SAP Product Standard(产品标准).这些产品标准,包含在SAP项目实施中大显 ...
- leetcode 127 单词接龙
给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度.转换需遵循如下规则: 每次转换只能改变一个字母. 转换过程中的中 ...
- 4个Linux服务器监控工具
下面是我想呈现给你的4个强大的监控工具. htop – 交互式进程查看器 你可能知道在机器上查看实时进程的标准工具top.如果不知道,请运行$ top看看,运行$ man top阅读帮助手册. hto ...
- (转)SpringMVC学习(一)——SpringMVC介绍与入门
http://blog.csdn.net/yerenyuan_pku/article/details/72231272 SpringMVC介绍 SpringMVC是什么? SpringMVC和Stru ...
- Maven添加本地依赖
在写本文的时候先来说明一下maven依赖的各种范围的意思 compile(编译范围) compile 是默认的范围:如果没有提供一个范围,那该依赖的范围就是编译范围.编译范围依赖在所有的c ...