题目描述:

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

解题思路:

这道题目由于是求所有的全排列,比较直观的方法就是递归了

class Solution:
# @param num, a list of integer
# @return a list of lists of integers def permute(self,l):
if(len(l)<=1):
return [l]
r=[]
for i in range(len(l)):
s=l[:i]+l[i+1:]
p=self.permute(s)
for x in p:
r.append(l[i:i+1]+x)
return r s = Solution()
print s.permute([1,2,3])

C++版

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

【leetcode】Permutations的更多相关文章

  1. 【LeetCode】Permutations 解题报告

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

  2. 【LeetCode】Permutations II 解题报告

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

  3. 【leetcode】Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  4. 【leetcode】Permutations (middle)

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  5. 【leetcode】Permutations II (middle)

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

  6. 【LeetCode】Permutations(全排列)

    这道题是LeetCode里的第46道题. 题目要求: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3 ...

  7. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. 彻底理解js中this的指向,不必硬背。

    首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...

  2. 【BZOJ-2668】交换棋子 最小费用最大流

    2668: [cqoi2012]交换棋子 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1055  Solved: 388[Submit][Status ...

  3. 11月6日下午PHP注册审核(审核状态控制登录、可以更改审核状态)

    1.创建登录界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  4. (转)为什么所有浏览器的userAgent都带Mozilla

    转自:http://www.eamonning.com/blog/view/289 以下是全文 最早的时候有一个浏览器叫NCSA Mosaic,把自己标称为NCSA_Mosaic/2.0 (Windo ...

  5. 在Application中集成Microsoft Translator服务之获取访问令牌

    我在这里画了一张图来展示业务逻辑 在我们调用microsoft translator server之前需要获得令牌,而且这个令牌的有效期为10分钟.下表列出所需的参数和对于的说明 参数 描述 clie ...

  6. 一个类似宣传的H5页面

    趁着闲置 做了一个H5的页面 感觉不错. 具体效果如下 框架上我选择 zepto(其实这个可有可无,推荐用原生的最好) FullPage (感觉挺好用的一个全屏滚动插件 ) pageResponse ...

  7. 一个很全的VTK实例网址

    https://cmake.org/Wiki/VTK/Examples/Cxx#Visualization

  8. Java重点识记

    1.final修饰的类不能被继承,已经达到类层次中的最低层. 2.abstract修饰的类或成员方法,表明是抽象的,含有抽象方法的类必须说明是抽象类,必须派生出子类. 3.JAVA对逻辑与和逻辑或提供 ...

  9. APP注释代码

    <meta name="viewport" content="width=device-width,height=device-height,inital-scal ...

  10. 【SCOI2005】 最大子矩阵 BZOJ 1084

    Description 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. Input 第一行为n,m,k(1≤n≤100,1≤m≤2 ...