全排列

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].

题目意思:

给定一个集合,求全排列。

解题思路:

一般的话,有两种思路可以考虑。递归和DFS遍历树。

这里我只用了递归的方法,关于怎么建一棵树然后DFS,可以参考 这里

这对于练习对递归的理解真是个不错的题目。

以[1,2,3]举例,可以用纸笔画一画,分别以1,2,3为根,画出三棵树来,然后由根到每一个节点就对应着一个排列。

首先最外层肯定要有一个for循环来一次遍历这里面的元素决定出根节点,然后在循环中递归。

其中,有两点需要注意的:

  • 要设置一个bool型的visited数组,大小和num一样大,来标记num中对应的元素是否已经访问过了,对于还没有访问过的,我们才可以把它加到element里来。这样可以避免在树的不同层出现同一个数。
  • 在element已经完成了一次排列后,对element进行pop_back(),然后将pop出来的数对应的visited设置为true,表示它现在可以被访问了。举例来说,当element中已经有了[1,2,3]并且添加到result里后,进行一个pop_back()剩下[1,2]并将visited[2]置为true,然后跳到上一层调用,再pop_back()剩下[1]并将visited[1]置为true,此时for循环i++,此时visited[2]为true,即向element里添加num[2],element变成[1,3],接着再调用递归,在下一层因为visited[0]为false,visited[1]为true,element将添加num[1]变成[1,3,2]。

对递归不熟的朋友,可以一边对着代码,一边用笔画一下几个树,过几遍就OK拉!

代码如下:

 #include <vector>
#include <iostream>
using namespace std; class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int>> result;
vector<int> element; int len = num.size();
bool *visited = new bool[len];
memset(visited,false,len); helper(num,result,element,visited);
return result;
} private:
void helper(vector<int> &num,vector<vector<int>> &result,vector<int> &element,bool *visited){
if(element.size() == num.size()){
//element中元素和num中元素一样多,说明得到了一个全排列,放进result里
result.push_back(element);
return ;
} for(int i = ; i < num.size(); i++){
if(!visited[i]){
visited[i] = true;
element.push_back(num[i]);
helper(num,result,element,visited); element.pop_back();
visited[i] = false;
}
}
}
}; int main(){
Solution s;
vector<int> num;
num.push_back();
num.push_back();
num.push_back();
s.permute(num);
}

【LeetCode练习题】Permutations的更多相关文章

  1. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

  2. Java for LeetCode 047 Permutations II

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

  3. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. Leetcode练习题Remove Element

    Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...

  6. [LeetCode] 47. Permutations II 全排列 II

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

  7. LeetCode 46 Permutations(全排列问题)

    题目链接:https://leetcode.com/problems/permutations/?tab=Description   Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...

  8. [LeetCode] 47. 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 ...

随机推荐

  1. cf472A Design Tutorial: Learn from Math

    A. Design Tutorial: Learn from Math time limit per test 1 second memory limit per test 256 megabytes ...

  2. WebBot - Build Web Apps as Easily as Native Ones - Home

    Python-based Template Packages Python-based Template Packages WebBot - Build Web Apps as Easily as N ...

  3. hdu 5610 Baby Ming and Weight lifting

    Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(the weight of which c ...

  4. 获取某月第一天,最后一天的sql server脚本

    本文来自:http://blog.csdn.net/chaoowang/article/details/9167969 这是计算一个月第一天的SQL 脚本:   SELECT DATEADD(mm, ...

  5. 兼容各个浏览器的H.264播放: H.264+HTML5+FLOWPLAYER+WOWZA+RMTP

    一.方案确定 计划做视频播放,要求可以播放H264编码的mp4文件,各个浏览器,各种终端都能播放. 首先查找可行性方案, http://www.cnblogs.com/sink_cup/archive ...

  6. 通过ant脚本编译打包android工程

    通过ant脚本,编译打包android工程 1.Android程序编译.打包.签名.发布的三种方式:  方式一:命令行手动编译打包  方式二:使用ant自动编译打包  方式三:使用eclipse+AD ...

  7. minicom与USB转串口

    实验器材:mini6410 连接方式:ARM板通过USB转串口线连接到pc机 下面是具体的设置了. 默认情况下,UBUNTU安装了USB转串口驱动(pl2303). 1.# lsmod | grep ...

  8. JQ选择器

    jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个 ...

  9. LINQ 操作符

    using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace LinQ ...

  10. NSArray数组的学习总结

    1.不可变数组NSArray NSArray是不可变的,而且只能储存Object-c对象.另外,数组的最后一个元素一定是nil,表示结束. 注:这些集合类只能收集cocoa对象(NSOjbect对象) ...