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

求全排列,相当于回溯法中的排列树。

public class Solution {
public List<List<Integer>> permute(int[] nums) {
HashSet<List<Integer>> res = new HashSet<List<Integer>>();
backtrace(0,nums.length,nums,res);
return new ArrayList(res);
}
public void backtrace(int t,int n,int[] nums,HashSet<List<Integer>> res){
if(t>=n){
List<Integer> list = new ArrayList<Integer>();
for(int i = 0;i<n;i++)
list.add(nums[i]);
res.add(list);
}
else{
for(int i = t ;i<n; i++){
swap(nums,i,t);
backtrace(t+1, n, nums, res);
swap(nums,t,i);
}
}
} public int[] swap(int[] ints, int x, int y) { int temp = ints[x];
ints[x] = ints[y];
ints[y] = temp;
return ints;
}
}

leetcode Permutation的更多相关文章

  1. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. [LeetCode] Permutation in String 字符串中的全排列

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...

  3. [leetcode]Permutation Sequence @ Python

    原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...

  4. LeetCode: Permutation Sequence 解题报告

    Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...

  5. leetcode -- permutation 总结

    leetcode上关于permutation有如下几题 Permutation Sequence Next Permutation Permutations Permutations II

  6. LeetCode Permutation in String

    原题链接在这里:https://leetcode.com/problems/permutation-in-string/description/ 题目: Given two strings s1 an ...

  7. LeetCode——Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. [Leetcode] Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  9. 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. eclipse的优化 gc.log

    原帖:http://www.javaeye.com/topic/756538 性能优化从身边做起. 首先建立评估体系,将workspace里所有的项目close掉,关闭eclipse.优化的用例就是启 ...

  2. cf478D Red-Green Towers

    D. Red-Green Towers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. Dubbo、Zookeeper、SpringMVC的整合使用

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  4. linux查看CPU高速缓存(cache)信息

    一.Linux下查看CPU Cache级数,每级大小 dmesg | grep cache 实例结果如下: 二.查看Cache的关联方式 在 /sys/devices/system/cpu/中查看相应 ...

  5. cc150 Chapter 2 | Linked Lists 2.5 add two integer LinkedList, return LinkedList as a sum

    2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...

  6. Multiple outputs from T4 made easy – revisited » DamienG

    Multiple outputs from T4 made easy – revisited » DamienG Multiple outputs from T4 made easy – revisi ...

  7. autoprefixer安装或者里sass的$mixin处理浏览器前缀

    Autoprefixer是一个后处理程序,不象Sass以及Stylus之类的预处理器.它适用于普通的CSS,可以实现css3代码自动补全.也可以轻松跟Sass,LESS及Stylus集成,在CSS编译 ...

  8. VF(动态规划)

    VF 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 Vasya is the beginning mathematician. He decided to make a ...

  9. [每日一题] OCP1z0-047 :2013-08-18 禁用启用约束――主键与外键 ..................................61

    正确答案:C 根据题意,测试结果如下: 1.创建表emp,并且设emp_no字段为主键,设mgr_no字段为外键. gyj@MYDB> create table emp 2 (emp_no nu ...

  10. python中的tab补全功能添加

    用Python时没有tab补全还是挺痛苦的,记录一下添加该功能的方法利人利己 1. 先准备一个tab.py的脚本 shell> cat tab.py #!/usr/bin/python # py ...