全排列

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. C语言的本质(33)——GCC编译器入门

    GCC(GNU CompilerCollection,GNU编译器套装),是由 GNU 开发的编程语言编译器.它是以GPL许可证所发行的自由软件,也是 GNU计划的关键部分.GCC原本作为GNU操作系 ...

  2. Train Problem I(栈)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. DM6437 C64X+ EDMA 疑惑总结记录

    总结一下DM6437中的EDMA的使用出现的问题,方便以后再开发定位问题. 1.EDMA Link 和 Chain的区别 link实现了DMA的自动重加载(非静态模式),需要两个param chain ...

  4. nodejs实现接收Snmp的Trap消息

    var assert = require('assert'); var ASN1 = { EOC: 0, Boolean: 1, Integer: 2, BitString: 3, OctetStri ...

  5. 简单实现div+css页面自适应

    Step1.在<head>添加如下代码<meta name="viewport" content="width=device-width, initia ...

  6. oracle取分组的前N条数据

    select * from(select animal,age,id, row_number()over(partition by animal order by age desc) row_num ...

  7. iOS学习笔记-死锁deadlock理解

    1.首先看一下官方文档的解释,这个block的队列是同步执行的,不像异步,这个方法直到block执行完毕才会返回 2.主线程一旦开启,就要先把自己的代码执行完成之后,才去执行加入到主队列中的任务 De ...

  8. 【转】VS2010中文注释带红色下划线的解决方法

    转载自:http://blog.csdn.net/whatday/article/details/7856323 环境:Visual Studio 2010 问题:代码中出现中文后会带下划线,很多时候 ...

  9. 自己写的轻量级PHP框架trig与laravel5.1,yii2性能对比

    看了下当前最热门的php开发框架,想对比一下自己写的框架与这些框架的性能对比.先看下当前流行框架的投票情况. 看结果对比,每个测试脚本做了一个数据库的联表查询并进行print_r输出,查询的sql语句 ...

  10. eclipse中的debug的用法

    最基本的操作是: 1.首先在一个java文件中设断点,然后debug as-->open debug Dialog,然后在对话框中选类后--> Run 当程序走到断点处就会转到debug视 ...