全排列

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. 剑指offer-面试题16.反转链表

    题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后的头结点 链表结点定义如下: struct ListNode { int m_nKey; ListNode* m_pNext; } 其实 ...

  2. Unity 手指触摸的方向(单手)

    最近写了一个跑酷游戏,总结下里面的知识点:O(∩_∩)O~ using UnityEngine; using System.Collections; public class Demo : MonoB ...

  3. C#主要字典集合性能对比[转]

    A post I made a couple days ago about the side-effect of concurrency (the concurrent collections in ...

  4. 三、Mp3帧分析(数据帧)

    一. 帧 帧头长4字节,是的,是4个字节,共32位. 帧头后面可能有两个字节的CRC 校验,这两个字节的是否存在决定于FRAMEHEADER 信息的第16bit, 为0 则帧头后面无校验,为1 则有校 ...

  5. .NETFramework类库

    .NET Framework 包括可加快和优化开发过程并提供对系统功能的访问的类.接口和值类型. 为了便于语言之间进行交互操作,大多数 .NET Framework 类型都符合 CLS,因而可在编译器 ...

  6. JS 不定函数参数argument的用法

    本篇文章只要是对js的隐含参数(arguments,callee,caller)使用方法进行了介绍. arguments arguments 该对象代表正在执行的函数和调用它的函数的参数. [func ...

  7. [原创] Assistant editor 取消拖拽 binding 的 UI 元素

    1. press up-right button "show the utilities" 2. press "show the Connections inspecto ...

  8. C#中大List的内存分配

    之前在开发中只用到List的时候几乎就是拿过来就用,从来没有考虑过List的内存分配问题,试想一个有10万元素的List的在构造和添加元素时内存是如何变化的呢?在MSDN上关于List的Capacit ...

  9. php.ini配置

    PHP作为一门强大的脚本语言被越来越多的web应用程序采用,不规范的php安全配置可能会带来敏感信息泄漏.SQL注射.远程包含等问题,规范的安全配置可保障最基本的安全环境.下面我们分析几个会引发安全问 ...

  10. PHP基础之 define() 函数

    定义和用法 define() 函数定义一个常量. 常量类似变量,不同之处在于: 在设定以后,常量的值无法更改 常量名不需要开头的美元符号 ($) 作用域不影响对常量的访问 常量值只能是字符串或数字 语 ...