Given a collection of distinct 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],
[3,2,1]
] 思路:
首先,固定第一个数字,递归求后序数组的全排列,比如example中,递归过程为:固定1,求[2,3]的全排列,然后固定2,求[3]的全排列,固定[3]求,空数组的全排列,由于此时数组为空,所以将结果保存,返回。
然后交换位子,继续进行递归,说的有点不是很明白,看一张图就比较清晰了。如图是一颗枚举树。现在的问题是怎么出现1,2,3开头的节点呢?就是使用交换,比如1和2交换,得到[2,1,3]固定2,求[1,3]的全排列。
那交换以后怎么得到3开头的呢?由于是递归,所以可以交换回1,2然后再交换1,3得到3开头的。

代码

class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> result;
permuteRec( 0, nums, result);
return result;
} private:
void permuteRec( int start, vector<int> &nums, vector<vector<int>> &result ){
if( start >= nums.size()){
result.push_back(nums);
return;
}
for( int i = start; i < nums.size(); i++ ){
       swap( nums[start], nums[i] );
permuteRec( start+1, nums, result);
swap( nums[start], nums[i] );
}
}
};

LeetCode 【46. Permutations】的更多相关文章

  1. [Leetcode][Python]46: Permutations

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...

  2. LeetCode:46. Permutations(Medium)

    1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...

  3. LeetCode 【47. Permutations II】

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

  4. 【线段树】【4-6组队赛】Problem H

    Problem Description #include <iostream> #include <algorithm> using namespace std; int n, ...

  5. LeetCode【第一题】Two Sum

    准备刷一刷LeetCode了. 题目: ''' Given an array of integers, return indices of the two numbers such that they ...

  6. 知乎上的一些文章---leetcode【笔记1】

    张土汪 http://github.com/shawnfan Java{script}代码仔 42 人赞同 [1.19.2017] 更新: 2017年1月17日, 陪我征战多年的 2014 MackB ...

  7. leetcode【14题】Longest Common Prefix

    题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...

  8. LeetCode【110. 平衡二叉树】

    对于平衡二叉树,就是左右深度相差1 就可以另外弄一个函数,计算深度,然后, 在原函数上进行比较深度是否相差1,再输出true or false. 至于迭代就可以,比较完左右节点,再比较各自的左右节点. ...

  9. leetcode 【Rotate List 】python 实现

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...

随机推荐

  1. wampserver You don't have permission to access / on this server. 解决 方法(转,正好碰到这样的事情了就转下来)

    最近在安装最近版wampserver 2.2 d时发现安装好后启动服务器,访问localhost显示You don't have permission to access / on this serv ...

  2. centos 安装redis自启动要点

    1.redis.conf a.daemonize yes b.pidfile /var/run/xxx.pid 2./etc/init.d/redis //加了下面三个注释部分,才支持设置开机自启动 ...

  3. Element is not currently visible and so may not be interacted with错误

    用selenium定位时,碰到这种错误的原因: 此种问题的关键是在于用className和id都不唯一所以找不到对象 所以,碰到这种问题就换一种定位方式

  4. ubuntu访问 windows文件

    在unbunt下,想打开windows的文件,出现这个报错 安装ntfs-3g: sudo apt-get install ntfs-3g 看下自己要挂载的分区叫啥名 sudo fdisk -l 我的 ...

  5. BBC.万物与虚无.Everything.and.Nothing

    这么有意思的纪录片怎么能错过 待续~

  6. 实现文本框默认灰色文字,点击消失,如果没输入内容可再返回原来的灰色文字(js版)

    $(document).ready(function(){ $("#biaoqian").val('这里是默认的灰色文字'); $("#biaoqian").c ...

  7. Physx入门

    [疑问] 1.Physx中的场景有大小的概念么?如果有大小,那么场景中的刚体超出场景边界之后, 如何定义之后的行为. 2.如何给一个刚体增加动量?目前的接口只看到设置速度或者增加作用力.

  8. Load Runner11录制脚本出现乱码的解决方法

    方法一: 1.录制的脚本出现乱码 Go to Vugen -> Tools -> Recording Options -> Advancedb)   Check the option ...

  9. docker初学笔记

    什么是docker 不准确的说,docker是一种轻量级的虚拟机,它把可执行文件和运行环境打包成一个image,放在容器里运行,但是启动速度比虚拟机快很多,资源消耗小.这种技术主要是为了解决部署环境的 ...

  10. Linux设备驱动中的并发控制

    1.并发是指多个执行单元同时.并行的执行.并发的执行单元对共享资源的访问很容易导致竞态. 在 Linux 内核中,主要的竞态发生于如下几种情况: ①对称多处理器(SMP)的多个 CPU ②单CPU内进 ...