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. iftop实时监控网络流量

    需要安装,linux自身不自带该命令 中间的<= =>这两个左右箭头,表示的是流量的方向. TX:发送流量 RX:接收流量 TOTAL:总流量 Cumm:运行iftop到目前时间的总流量 ...

  2. error LNK2005:"private:__thiscall编译错误

    对于这种编译错误,网上给出了很多解决办法,大部分都是忽略特定库,或者改变多线程调试DLL,但是均没有效果. 这里记录下自己的解决方法,首先按照下图,取消从父级或项目默认设置继承,避免与其他库中的定义冲 ...

  3. JS的同步和异步加载

    引言 JS的“加载”不能理解为下载,它是分为两个部分:下载,执行.默认的JS加载是同步的,因为浏览器需要一个稳定的DOM结构,而执行JS时可能会对DOM造成改变,所以在执行JS时一定会阻塞HTML的渲 ...

  4. rdf(资源描述框架)

    资源描述框架(Resource Description Framework),一种用于描述Web资源的标记语言.RDF是一个处理元数据的XML(标准通用标记语言的子集)应用,所谓元数据,就是“描述数据 ...

  5. 提交代码出现 Push to origin/master was rejected 错误解决方法

    转至博客:http://www.xtyos.cn/archives/qt-1-index 为什么会出现这样的问题 一般发生在 GitHub 或 码云 刚刚创建仓库第一次pull的时候,两个仓库的差别非 ...

  6. IO流 复制文件及文件夹

    package io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; im ...

  7. python-基础-面象对象

    1 类和对象 定义类 定义一个类,格式如下: class 类名: 方法列表 demo:定义一个Car类 # 定义类 class Car: # 方法 def getCarInfo(self): prin ...

  8. 在PyCharm中导入Numpy和Pygame模块 (win8.1)

    我用的是anaconda安装python3.6 已经在终端 pip install numpy 但是在pycharm运行程序出现错误:ImportError: No module named nump ...

  9. T2483 电梯(模拟题)

    https://www.luogu.org/problem/show?pid=T2483 题目背景 开启了升降梯的动力之后,探险队员们进入了升降梯运行的那条竖直的隧道,映入眼帘的是一条直通塔顶的轨道. ...

  10. Qt运行不出现界面

    安装Qt之后按照例程运行,结果不出现界面,原因是路径中有中文,将中文全部改成英文之后,问题解决.