这道题是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(全排列)的更多相关文章

  1. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  2. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  3. [CareerCup] 9.5 Permutations 全排列

    9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...

  4. 每日一题-——LeetCode(46)全排列

    题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...

  5. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  6. LeetCode:全排列【46】

    LeetCode:全排列[46] 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2 ...

  7. LeetCode 47——全排列 II

    1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...

  8. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  9. [LeetCode] 46. Permutations 全排列

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  10. [leetcode]46. Permutations全排列(给定序列无重复元素)

    Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...

随机推荐

  1. Web Scalability for Startup Engineers Tip&Techniques for Scaling You Web Application --读书笔记

    Web Scalability for Startup Engineers Tip&Techniques for Scaling You Web Application 第1章和第2章讲述可伸 ...

  2. nodejs 不是单线程

    nodejs 不是单线程 在我机器上,nodejs 起了近 20 个线程. 对,你没有看错,20个线程.

  3. netty-socketio即时通讯

    jar包和依赖包在360云盘中:所有文件 > 学习 > jar包 > netty-socketio-1.7.10以及依赖 原文链接:http://www.cnblogs.com/al ...

  4. html5.0学习记录(一)——可拖动视频播放器

    最近自己在重新学习html5新特性,了解到有视频标签和拖动标签,于是自己用这两个特性写了一个小demo,主要功能就是可以通过拖动视频来直接播放.效果图如下: 页面使用了<video>标签和 ...

  5. PHP 根据两点的经纬度计算距离

    /** * @name 根据经纬度确定两点的距离[地理位置] * @author tbj * @param float $lat 纬度值 * @param float $lng 经度值 * @date ...

  6. HDU 4044 GeoDefense (树形DP,混合经典)

    题意: 给一棵n个节点的树,点1为敌方基地,叶子结点都为我方阵地.我们可以在每个结点安放炸弹,每点至多放一个,每个结点有ki种炸弹可选,且每种炸弹有一个花费和一个攻击力(1点攻击力使敌人掉1点hp). ...

  7. codevs 3026 恶心的扑克

    时间限制: 1 s  空间限制: 64000 KB  题目等级 : 白银 Silver 题目描述 Description 有一副恶心的扑克,从小到大依次是3 , 4 , 5 , 6 , 7 , 8 , ...

  8. git快速入门(MAC系统,github,ssh key)

    如果使用过svn的话,git大致可以认为是多了本地库的svn.git先本地提交commit到本地库,然后再push到远程服务器的库.git是分布式的代码管理工具,基于SSH协议.ssh的作用就是为了不 ...

  9. WPF中窗体在同一个位置实现不同页面切换

    要想在WPF窗体中实现不同页面切换,我们就需要用到ContentControl这个控件,这个控件的位置和大小就是你要显示页面的位置和大小. 下面举例说明: Xaml: <Grid> < ...

  10. struts1标签库

    Struts提供了五个标签库,即:HTML.Bean.Logic.Template和Nested. HTML标签 : 用来创建能够和Struts 框架和其他相应的HTML 标签交互的HTML 输入表单 ...