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. Scrum Meeting 3-20151203

    任务安排 姓名 今日任务 明日任务 困难 董元财 请假(明天是编译截至最后一天) 学习上拉加入新的listview 无 胡亚坤 请假(明天是编译截至最后一天) 设计优化聊天页面 无 刘猛 请假(明天是 ...

  2. python去除空格和换行符的方法

    一.去除空格 strip() " xyz ".strip() # returns "xyz" " xyz ".lstrip() # retu ...

  3. 转:C语言 可变参数

    C语言 可变参数 堆栈一般是怎么压栈处理的 /* * stack space: * *        参数3   |    up *        参数2   | *        参数1   v   ...

  4. IAR更改代码字体&快速模板设置。——Arvin

    1.是用软件提供的字体 如果只想简单的设置,可进行如下设置Tools->IDE Options->Editor->Colors and Fonts->Editor Font-& ...

  5. pfile 与 spfile

    启动方式与顺序: 启动顺序:dbs 下的 init --> dbs 下的 spfile 如果 pfile 中没有指定 spfile 参数,那么数据库以 pfile 方式启动 如果 pfile 中 ...

  6. myeclipse中disable maven nature

    1.直接原因:出现这个问题,一般都是因为手抖误操作. 但是出现了问题,还不知道从何查起. 可能出现的场景是eclipse安装Maven插件后,右键项目却找不到Maven按钮,继而无法编译项目. 2.实 ...

  7. 腾讯云TDSQL审计原理揭秘

    版权声明:本文由孙勇福原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/244 来源:腾云阁 https://www.qclo ...

  8. 使用File类递归列出E盘下全部文件

    import java.io.File;public class FileListTest { public void tree(File file){ if(file.listFiles()!=nu ...

  9. python之map、filter、reduce、lambda函数 转

    python之map.filter.reduce.lambda函数  转  http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...

  10. java 多线程—— 线程让步

    java 多线程 目录: Java 多线程——基础知识 Java 多线程 —— synchronized关键字 java 多线程——一个定时调度的例子 java 多线程——quartz 定时调度的例子 ...