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. utils05_git在idea下的操作

    1.idea下将工程添加到本地仓库 1>找到自己的git.exe 2>创建本地的git仓库,将项目放入本地仓库 3> *从本地仓库更新 *提交到本地仓库 *比较版本差异 *丢弃我的修 ...

  2. [转载] DDK中VPORT Mini-Driver的使用说明

    学习下. 原文地址:DDK中VPORT Mini-Driver的使用说明作者:跳皮筋的小老鼠 要使用TI DDK中实现的VPORT驱动程序,首先需要在程序中提供VPORT_PortParams类型的参 ...

  3. Ajax的简单基础

    什么是 AJAX ? AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新. 这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行 ...

  4. redis学习笔记06-主从复制和哨兵机制

    1.主从复制 为了保证线上业务的持续运行,防止主节点因宕机而重启数据恢复消耗太长时间,通常会准备一个备用节点,备份主节点的数据,当主节点出问题时立马顶上.这种机制就叫做主从复制.在了解redis的主从 ...

  5. C/C++ - 类中成员变量是引用

    C++引用 引用在定义时必须初始化,否则编译时便会报错.如果类(自定义类型)的成员是引用类型,需要注意一些问题. 引用成员变量 并不为这个变量新辟空间:类对象做成员变量则是要对其新辟一段空间的 不能有 ...

  6. java内部类和静态内部类

    看代码理解内部类,局部内部类和静态内部类 内部类代码: public class InnerTest { private static String name; private int age; pu ...

  7. Shell脚本编程中截取字符串方法

    例如: 假设变量var=http://www.baidu.com/111.png 1.#号截取(删左留右) echo ${var#*//} # 号是运算符,*// 表示从左边开始删除第一个 // 号及 ...

  8. bzoj 1034 [ZJOI2008]泡泡堂BNB——贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1034 原来觉得和 bzoj4977跳伞求生 有点像(虽然还没做). 所以对于a[ ]从小到大 ...

  9. angular1.0 $http jsonp callback

    $http.jsonp(sDUrl,{cache:false,jsonpCallbackParam:'callback'}); https://stackoverflow.com/questions/ ...

  10. For循环和闭包问题

    考虑一下以下的代码片段: for (var i = 0; i < 5; i++) { var btn = document.createElement('button'); btn.append ...