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. 健康管理app要注意哪些要点

    健康管理app现在变得越来越热,越来越多的垂直领域的加入,让健康app的市场逐渐扩大,但移动健康管理app仍有许多缺陷需要解决.健康管理并不是治病,而是让健康的人更好的保持身体健康状态,让慢性病高风险 ...

  2. STL中,迭代器的分类

    五类迭代器如下: 1.输入迭代器:只读,一次传递    为输入迭代器预定义实现只有istream_iterator和istreambuf_iterator,用于从一个输入流istream中读取.一个输 ...

  3. unmount的时候报错

    卸载存储的时候报错 device is busy 解决办法 例:/mnt/test 为存储挂载点 fuser -m -v /mnt/test fuser 可以显示出当前哪个程序在使用磁盘上的某个文件. ...

  4. poj 2356 Find a multiple(鸽巢原理)

    Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...

  5. 什么是FastCGI?

    什么是FastCGI? PHP的FastCGI使你的所有php应用软件通过mod_fastci运行,而不是mod_phpsusexec.FastCGI应用速度很快 是因为他们持久稳定.不必对每一个请求 ...

  6. 键盘code码速查表

    键盘 Key Code对照表 字母和数字键的键码值(keyCode) 按键 键码 按键 键码 按键 键码 按键 键码 A 65 J 74 S 83 1 49 B 66 K 75 T 84 2 50 C ...

  7. Samsung K9F1G08U0D SLC NAND FLASH简介(待整理)

    Samsung  K9F1G08U0D,数据存储容量为128M,采用块页式存储管理.8个I/O引脚充当数据.地址.命令的复用端口.详细:http://www.linux-mtd.infradead.o ...

  8. Android设备定制为永不锁屏

    Android系统的锁屏时间存放在Setting数据库中,字段为Settings.System.SCREEN_OFF_TIMEOUT.查看 DatabaseHelper.java文件可以找到: fra ...

  9. 使用JUnit单元测试入门

    一.JUnit是什么? JUnit是一个开发源代码的java测试框架,用于编写和运行可重复的测试.它是用于单元测试框架体系xUnit的一个实例(用于java语言).JUnit最初是由Erich Gam ...

  10. Android多媒体开发-- android中OpenMax的实现整体框架

    1.android中用openmax来干啥? android中的AwesomePlayer就 是用openmax来做(code)编解码,其实在openmax接口设计中,他不光能用来当编解码.通过他的组 ...