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 ... ...
随机推荐
- JAVA虚拟机内存架构
Java在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途.创建和销毁的时间,有一些是随虚拟机的启动而创建,随虚拟机的退出而销毁,有些则是与线程一一对应,随 ...
- Tomcat跨二级域配置
内容转自:http://blog.csdn.net/luka2008/article/details/38385703/,请直接看原文,不过这篇“原文”也是转的... 1,Tomcat下 代码: im ...
- A. Dreamoon and Stairs(Codeforces Round #272)
A. Dreamoon and Stairs time limit per test 1 second memory limit per test 256 megabytes input standa ...
- redis lpop key 当key不存在时,返回nil , 监测redis执行语句是否正常执行
Lpop key 返回值: 列表的头元素. 当key 不存在时, 返回 nil . 需求: 开发在执行 lpop key 时, 出现问题 , 执行语句卡住, 不能执行下去 , 需对此做一个监测 由于 ...
- NYOJ-------三角形
Problem A 三角形 时间限制:1000 ms | 内存限制:65535 KB 描述 在数学中,如果知道了三个点的坐标,我们就可以判断这三个点能否组成一个三角形:如果可以组成三角形,那么 ...
- python练习笔记——丑数的计算
只包含因子2,3,5的正整数被称作丑数,比如4,10,12都是丑数,而7,23,111则不是丑数,另外1也不是丑数.——摘自百度百科 get_num = int(input("请您输入丑数的 ...
- Spring.net(二)----初探IOC容器
我在上一篇关于Spring.net的文章“Spring.NET框架简介及模块说明 ”中很详细的介绍了,本文就不旧话从提.我门就直奔主题吧. 1.首先了解两个接口. IObjectFactory接口和 ...
- Oschina 安卓client源代码学习之中的一个
今天主要研究一下两个功能 (1)双击返回键退出程序 (2)接近完美地退出程序 (1) 在非常多应用程序里都有一个功能,就是点击返回键,之后提示你再点击返回键就退出程序. 之前一直非常好奇这是怎么实现的 ...
- Dev BarManager使用方法
作者:jiankunking 出处:http://blog.csdn.net/jiankunking 近期使用BarManager时候.发现一个问题就是在一開始把BarManager控件拖到窗口上的时 ...
- Python直接赋值、浅拷贝和深度拷贝解析
直接赋值:其实就是对象的引用(别名). 浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象. 深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象 ...