LeetCode: Permutations 解题报告
Permutations
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].
SOLUTION 1:
经典的递归回溯题目,一次ACCEPT. 请也参考上一个题目LeetCode: Combinations 解题报告.
public class Solution {
public List<List<Integer>> permute(int[] num) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
if (num == null || num.length == 0) {
return ret;
} dfs(num, new ArrayList<Integer>(), ret);
return ret;
} public void dfs(int[] num, List<Integer> path, List<List<Integer>> ret) {
int len = num.length;
if (path.size() == len) {
ret.add(new ArrayList<Integer>(path));
return;
} for (int i = 0; i < len; i++) {
if (path.contains(num[i])) {
continue;
} path.add(num[i]);
dfs(num, path, ret);
path.remove(path.size() - 1);
}
}
}
SOLUTION 2:
可能有的同学觉得为什么path.contains不用hashmap来代替哩?所以主页君写了一个带hashmap的版本。结论是,在这个set规模小的时候,hashmap的性能还不
如arraylist。
原因可能在于,hashmap申请的不是一个连续的空间,而arraylist比较小的话,直接在连续内存中操作,速度会比较快。
以下是此程序的运行结果,hashmap的版本速度要慢一倍:
Test size:9
Computing time with HASHMAP: 629.0 millisec.
Test size:9
Computing time with list: 310.0 millisec.
package Algorithms.permutation; import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap; public class Permutation {
public static void main(String[] strs) {
int[] num = {1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.printf("Test size:%d \n", num.length); Stopwatch timer1 = new Stopwatch(); permute(num);
System.out
.println("Computing time with HASHMAP: "
+ timer1.elapsedTime() + " millisec."); System.out.printf("Test size:%d \n", num.length); Stopwatch timer2 = new Stopwatch(); permute2(num);
System.out
.println("Computing time with list: "
+ timer2.elapsedTime() + " millisec.");
} public static ArrayList<ArrayList<Integer>> permute(int[] num) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
if (num == null) {
return ret;
} permuteHelp(num, ret, new LinkedHashMap<Integer, Integer>());
return ret;
} public static void permuteHelp(int[] num, ArrayList<ArrayList<Integer>> ret, LinkedHashMap<Integer, Integer> set) {
if (set.size() == num.length) { ArrayList<Integer> list = new ArrayList<Integer>();
for (Integer i: set.keySet()){
list.add(i);
}
ret.add(list);
return;
} int len = num.length;
for (int i = 0; i < len; i++) {
if (set.containsKey(num[i])) {
continue;
} //path.add(num[i]);
set.put(num[i], 0);
permuteHelp(num, ret, set);
//path.remove(path.size() - 1);
set.remove(num[i]);
}
} public static ArrayList<ArrayList<Integer>> permute2(int[] num) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
if (num == null) {
return ret;
} ArrayList<Integer> path = new ArrayList<Integer>();
permuteHelp2(num, path, ret);
return ret;
} public static void permuteHelp2(int[] num, ArrayList<Integer> path, ArrayList<ArrayList<Integer>> ret) {
if (path.size() == num.length) {
ret.add(new ArrayList<Integer>(path));
return;
} int len = num.length;
for (int i = 0; i < len; i++) {
if (path.contains(num[i])) {
continue;
} path.add(num[i]);
permuteHelp2(num, path, ret);
path.remove(path.size() - 1);
}
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/dfs/Permute.java
LeetCode: Permutations 解题报告的更多相关文章
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【LeetCode】46. Permutations 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:库函数 方法二:递归 方法三:回溯法 日期 题目地址:h ...
- leetcode—Palindrome 解题报告
1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...
- LeetCode C++ 解题报告
自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- LeetCode: Subsets 解题报告
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- LeetCode: Triangle 解题报告
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- LeetCode: isSameTree1 解题报告
isSameTree1 Given two binary trees, write a function to check if they are equal or not. Two binary t ...
- LeetCode: Combinations 解题报告
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...
随机推荐
- cocos2d-x项目101次相遇-安装和环境搭建 -xcode
cocos2d-x 101次相遇 / 文件夹 1 安装和环境搭建 -xcode 2 Scenes . Director, Layers, Sprites 3 建立图片菜单 4 在 ...
- 〖Ruby〗Ruby关键字
模块定义:module 类定义:class 方法定义:def, undef 检查类型:defined? 条件语句:if, then, else, elsif, case, when, unless 循 ...
- 最快下载速度100Mbps!4G LTE技术全解析
1导读,关于4G的几个关键概念 [PConline资讯]100Mbps下载速度是什么概念?比3G网速快50倍又是什么概念?比3G通信方式更灵活.通信频谱更宽绰.通信质量更高效.通信费用更便宜是怎样一个 ...
- 解决python:'ascii' codec can't encode characters in position问题
今天把一个列表转换成字符串输出的时候出现了UnicodeEncodeError: 'ascii' codec can't encode characters in position 32-34: or ...
- Linux各主要发行版的包管理命令对照
Linux各主要发行版的包管理命令对照 Debian使用的是apt和dpkg,Gentoo则用的是emerge,Redhat的yum.Suse的zypper.Arch的pacman.Slackware ...
- invalid configuration x86_64-unknown-linux-gnu' machine x86_64-unknown' not recognized
转载自:http://blog.csdn.net/php_boy/article/details/7382998 前两天在装机器软件的时候, 出现了下面的错误, invalid configurati ...
- UltraEdit加入到右键菜单中
http://www.cppblog.com/prayer/archive/2009/02/20/74429.htmlUltraEdit安装好之后,拷贝到其它机器就可以直接使用而无需注册,但少了一个功 ...
- js重要函数
window.setTimeout(code,millisec) 方法用于在指定的毫秒数后调用函数或计算表达式.只执行 code 一次(比如某个界面是上左右的三个frame界面,右边这个界面要调用 ...
- 【Linux】X window与文本模式的切换
Linux默认的情况下会提供六个Terminal来让使用者登陆,切换的方式为:[Ctrl] + [Alt] + [F1]~[F6]的组合按钮.那这六个终端接口如何命名呢,系统会将[F1] ~ [F6] ...
- python练习笔记——用列表推导式生成二维列表
用列表推导式如何生成如下列表:[[1, 2, 3], [4, 5, 6], [7, 8, 9]] inner_list = [] outer_list = [] for i in range(1,10 ...