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

解题思路:

本题解题方法多多,为防止重复,决定使用Set的dfs算法,JVVA实现如下:

static public List<List<Integer>> permute(int[] nums) {
Set<List<Integer>> set=new HashSet<List<Integer>>();
dfs(set,nums,0);
return new ArrayList<List<Integer>>(set);
}
static List<Integer> list=new ArrayList<Integer>();
static public void dfs(Set<List<Integer>> set,int[] nums,int depth){
if(depth==nums.length){
set.add(new ArrayList<Integer>(list));
return;
}
for(int i=0;i<=depth;i++){
list.add(i,nums[depth]);
dfs(set,nums,depth+1);
list.remove(i);
}
}

Java for LeetCode 046 Permutations的更多相关文章

  1. Java for LeetCode 047 Permutations II

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

  2. LeetCode 046 Permutations 全排列

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

  3. LeetCode 046 Permutations

    题目要求:Permutations(全排列) Given a collection of numbers, return all possible permutations. For example, ...

  4. Java for LeetCode 060 Permutation Sequence

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

  5. 【LeetCode】Permutations 解题报告

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

  6. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  7. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  8. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  9. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

随机推荐

  1. 【POJ 1061】青蛙的约会

    题 Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要 ...

  2. 项目跑起来之后,一会儿后台就会报错Illegal access: this web application instance has been stopped already. Could not load [com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask]. The following stack trace

    一月 24, 2016 6:42:54 下午 org.apache.catalina.loader.WebappClassLoaderBase checkStateForResourceLoading ...

  3. spring AOP面向切面编程学习笔记

    一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...

  4. CSS 2D转换 matrix() 详解

    2D转换 IE10.Firefox.Opera 支持 transform 属性 Chrome.Safari 需要前缀 -webkit- . IE9 需要前缀 -ms- . translate():接收 ...

  5. mybatis中的resultMap

    resultMap <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBL ...

  6. 下载安装resin-3.X服务器并配置到myeclipse

    前提是先安装jdk,具体自己安装. 1.到resin官网http://www.caucho.com/download/下载相应压缩包,比如resin-3.2.0.zip 2.解压下载的resin-3. ...

  7. shell 删除文件下的* (copy).jpg备份文件

    shell编程中,  在for, while循环中为什么不用(), {} 不是没有; 而是因为(), {}做了其他用途: (): 执行命令组, 注意这个命令组是新开一个子shell中执行, 因此,括号 ...

  8. wordpress编辑主题时报错Warning: scandir() has been disabled for security reasons in

    在ubuntu下面安装了一个wordpress程序,在后台什么都没干,编辑主题时,发现页面中报下面的错误. notice: /home/wwwroot/test.localhost/wordpress ...

  9. IE8 松散耦合进程框架(Loosely-Coupled IE (LCIE)--特性介绍

    官方介绍:http://blogs.msdn.com/b/ie/archive/2008/03/11/ie8-and-loosely-coupled-ie-lcie.aspx 参考文档:http:// ...

  10. CSS简写指南

    高效的css写法中的一条就是使用简写.通过简写可以让你的CSS文件更小,更易读.而了解CSS属性简写也是前端开发工程师的基本功之一.今天我们系统地总结一下CSS属性的缩写. 色彩缩写 色彩的缩写最简单 ...