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. JS实现数据双向绑定

    本文参考https://www.cnblogs.com/tianhaining/p/8425345.html 首先先说个面试题哈,就是vue中的v-model是如何实现双向数据绑定的咳咳,下面开始背诵 ...

  2. etcd 研究研究

    先记录参考信息:etcd 场景https://blog.csdn.net/bbwangj/article/details/82584988 etcd 集群部署https://www.jianshu.c ...

  3. SpringMvc项目加载顺序及上下文小结

    前言: 使用springMvc已经三年了,但是内部原来一直不太了解,看到ServletConetxt和ApplicationContext头就大,趁着这几天学习,正好学习下相关的知识. 1.Servl ...

  4. bootstrap-table的简要使用

    bttable功能强大!同时支持申明方式和编程方式来使用!是wenzhixin主导开发一款开源的表格控件! 文档比较详尽,但要求初学者已经比较熟悉js,jquey等基本内容,否则可能许多代码无法阅读! ...

  5. IO流15 --- 数据流操作Java语言的基本数据类型 --- 技术搬运工(尚硅谷)

    写入数据 @Test public void test10() throws IOException { DataOutputStream dos = new DataOutputStream(new ...

  6. Ubuntu下使用sshfs挂载远程目录到本地

    访问局域网中其他Ubuntu机器,在不同机器间跳来跳去,很是麻烦,如果能够把远程目录映射到本地无疑会大大方面使用,就像Windows下的网络映射盘一样.在Linux的世界无疑也会有这种机制和方式,最近 ...

  7. 浏览器在IE8 以下时显示提示信息,提示用户升级浏览器

    <!--[if lt IE 8]> <div style="background: #eeeeee;border-bottom: 1px solid #cccccc;col ...

  8. JavaScript中用var和不用var的区别

    Javascript声明变量的,虽然用var关键字声明和不用关键字声明,很多时候运行并没有问题,但是这两种方式还是有区别的.可以正常运行的代码并不代表是合适的代码. varnum=1; 是在当前域中声 ...

  9. How To Install Nginx on CentOS 7(转)

    How To Install Nginx on CentOS 7 PostedJuly 22, 2014 427.4kviews NGINX CENTOS About Nginx Nginx is a ...

  10. hql 条件查询 返回空的一种情况

    为何会出现查询为空,但是查询整个表没问题的情况呢? 这里是没有分清字符串和变量 原来写的是, String hql = "from ClientInfoModel where clientI ...