题目描述:

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. 在php中验证复选框

    PHP接收多个同名复选框信息不像ASP那样自动转换成为数组,这给使用带来了一定不便.但是还是有解决办法的,就是利用javascript做一下预处理.多个同名复选框在javascript中还是以数组的形 ...

  2. 修改Firefox的User-Agent,伪装修改秘籍

    火狐浏览器修改userAgent的办法一: 在火狐浏览器地址栏输入"about:config",按下回车进入设置菜单. 找到"general.useragent.over ...

  3. oracle---plsql---示例laobai

    select * from scott.emp; --1 列出emp表中各部门的部门号,最高工资,最低工资 select deptno,max(sal),min(sal) from scott.emp ...

  4. easyUi 框架中的JS文件传递参数的区别

    1.情景一 //JS文件 ajax的请求url : parent.baseUrl+"user/customer/findOne/" + id, //后台JAVA代码接收参数 @Re ...

  5. Linux终端最常用快捷键

    新建终端窗口: crtl+shift+N 终端的漂移/切换:shift+左右箭头 挂 起:crtl+s 解除挂起:crtl+q 清 屏:crtl+l 命令行光标移动: crtl+a 移动到命令行首 c ...

  6. GIT本地配置和PUSH

    因为GIT使用的是LINUX命令,所以可以参考LINUX的相关命令 一.在本地配置好GIT仓库: 1.首先进入当前工程的目录:cd [filepath] (记住这里的目录应该用/,因为LINUX是这样 ...

  7. 深入理解javascript原型和闭包(7)——原型的灵活性

    在Java和C#中,你可以简单的理解class是一个模子,对象就是被这个模子压出来的一批一批月饼(中秋节刚过完).压个啥样,就得是个啥样,不能随便动,动一动就坏了. 而在javascript中,就没有 ...

  8. Bash 4.4 中新增的 ${parameter@operator} 语法

    Bash 4.4 中新增了一种 ${...} 语法,长这样:${parameter@operator}.根据不同的 operator,它展开后的值可能是 parameter 这个参数的值经过某种转换后 ...

  9. JavaScript类型判断instanceof与typeof对比

    经常有人会在JavaScript里写如下的方法: function checkType() { var s1 = 123; var s2 = "OK"; if (s1 instan ...

  10. T-SQL备忘-表连接更新

    1.update a inner join b on a.id=b.id set a.name=b.name where ...   2.update table1 set a.name = b.na ...