Given a collection of numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1].

Hide Tags

Backtracking

建立一棵树,比如说
                                       1234
 
               1234           2134           3214         4231     //就是swap(1,1)  swap(1,2) swap(1,3) swap(1,4)
                  |          
     1234  1324  1432                        //就是swap(2,2)  swap(2,3) swap(2,4) 
        |
  1234  1243                                  //就是swap(3,3)  swap(3,4) 
 
然后,就用DFS遍历,叶子节点就是我们想要的
class Solution {
private:
vector<vector<int> > ret;
public:
void perm(vector<int> num,int i){
if(i==num.size()){
ret.push_back(num);
return;
}
for(int j=i;j<num.size();j++){
swap(num[i],num[j]);
perm(num,i+);
swap(num[j],num[i]); //复原,进行下一个交换前需复原之前状态
}
}
vector<vector<int> > permute(vector<int> &num) {
perm(num,);
return ret;
}
};
 
 

Permutations 全排列 回溯的更多相关文章

  1. [CareerCup] 9.5 Permutations 全排列

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

  2. [LeetCode] 46. 全排列(回溯)

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

  3. [LeetCode] Permutations 全排列

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

  4. lintcode 中等题:permutations 全排列

    题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...

  5. permutations(全排列)

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

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

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

  7. [leetcode]47. Permutations全排列(给定序列有重复元素)

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

  8. LeetCode题解 Permutations II 和 Permutations I ——回溯算法

    这个算法感觉还是很陌生的.算法导论里没有讲这个算法,而数据结构与算法分析只用了一节来阐述.我居然跳过去了..尴尬. 笨方法解决的: 第一题: 给定一个元素不重复的数组,枚举出他们的全排列. 方法1:递 ...

  9. 46. Permutations (全排列)

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

随机推荐

  1. python中defaultdict方法的使用

    默认值可以很方便 众所周知,在Python中如果访问字典中不存在的键,会引发KeyError异常(JavaScript中如果对象中不存在某个属性,则返回undefined).但是有时候,字典中的每个键 ...

  2. 原 JEECMS导入IDEA进行二次开发图文教程

    JEECMS导入IDEA进行二次开发图文教程 2017年05月15日 17:03:53 Swain_Ho 阅读数 3257    版权声明:本文为博主原创文章,未经博主允许不得转载. https:// ...

  3. Maven实战04_使用Archetype生成项目骨架

    在上一章中的HelloWorld中,我们的项目遵循了一些Maven项目的约定 在项目的根目录中放置pom.xml 在src/main/java目录中放置项目的主代码 在src/test/java目录中 ...

  4. WPF 自适应布局控件

    public class KDLayoutGroup : Grid { public double LabelWidth { get; set; } public double GetLabelWid ...

  5. 免费提取百度文库 doc 文件

    首先说明,今天要推荐的这款软件,不能不能不能免费提取百度文库里 PDF 格式的文件. 对于其他的格式,无论收费与否都能免费提取. 只是口头说说免不了耍流氓的嫌疑,举栗如下: 百度文库里<喜迎党的 ...

  6. Eclipse配置jstl标准标签库详解

    安装JSTL1.2 日期:2017-06-27 下载jstl1.2版本,下载地址:http://repo2.maven.org/maven2/javax/servlet/jstl/ 用压缩包打开jst ...

  7. vue-i18n 的用法

    主要用于网站国际化,开发可以切换多语言的网站 1,安装 npm install vue-i8n 2,在main.js中引入和注册 import VueI18n from 'vue-i18n' impo ...

  8. mac 安装svn

    别人说用Xcode装,我也不知道我这个是不是用Xcode装的 在命令行界面输入 sudo bash svn --version 会出现一大段介绍,关于xcode的,我也不懂,一只敲空格键到最后,然后输 ...

  9. netbeans调试webapp 只能用localhost访问

    etbeans 我的电脑是192.168.0.2,用这个地址访问 网上有人说,分两种情况 此问题分两种情况: 1. 可以用127.0.0.1访问 2. 不能用127.0.0.1访问 针对第一种情况,我 ...

  10. 用navicat把MYSQL一张表的数据批追加到另外一张表

    1. 表结构完全一样 insert into 表1 select * from 表22. 表结构不一样(这种情况下得指定列名) insert into 表1 (列名1,列名2,列名3) select ...