Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
public class Permutations {

    public List<List<Integer>> permute(List<Integer> nums) {
List<List<Integer>> result = new ArrayList<>();
result.add(new ArrayList<>()); for(int num: nums) {
int sz = result.size();
for(int i = 0; i < sz; ++i) {
List<Integer> old = result.get(i);
for(int j = 0; j <= old.size(); ++j) {
List<Integer> updated = new ArrayList<>(old);
updated.add(j, num);
result.add(updated);
}
}
result.subList(0, sz).clear();
}
return result;
} public static void main(String[] args) {
Permutations p = new Permutations();
List<List<Integer>> result = p.permute(Arrays.asList(1, 2, 3));
System.out.println(result);
}
}

Time complexity: O(n * n!) = O(0! + 1! + 2! + 3! + ...n!)

Iterative: staring with an empty array, { { } }

Adding number 1, { { } } ->{ { 1 } }

Adding number 2, { { 1 }  } ->{ { 2 , 1 }, { 1, 2 } }

Adding number 3: { { 2, 1 }, { 1, 2 } } -> { {3, 2, 1}, {2, 3, 1}, {2, 1, 3}, {3, 1, 2}, {1, 3, 2}, {1, 2, 3}}

Taken-out: start with something small, build the solution based on smaller inputs.

Recursive: backtracking, swapping elements to get new array

1. {1, 2, 3}  swap the first element with the rest, i = 0

2. After 1 swapped with itsself, {1, 2, 3}, swap the 2nd element with the rest, pos = 1

3. After 2 swapped with itselft, {1, 2, 3}, swap the 3rd element with the rest, pos = 2

4. After 3 swapped with iteself, {1, 2, 3}, you can either return here when pos == nums.size() or pos > nums.size(), result.add(new ArrayList<>(nums)), as base case. {{1, 2, 3}}

5. Back to step 3, 2 swapped with 3, {1, 3, 2}, swap the 3rd element with the rest, pos = 2

6. After 2 swapped with iteself, {1, 3, 2}. { {1, 2, 3}, {1, 3, 2} }

7. Backtracked to step 3, after the subcall which swapped 2 and 3, in order to return the nums to previous state before the subcall, we need to swap the elemtns back.

public class Permutations {
private void permuteHelper(int pos, List<Integer> nums, List<List<Integer>> result) {
int len = nums.size();
if(pos == len-1) {
result.add(new ArrayList<>(nums));
return;
} for(int i = pos; i < len; ++i) {
Collections.swap(nums, pos, i);
permuteHelper(pos+1, nums, result);
Collections.swap(nums, pos, i);
}
} public List<List<Integer>> permute(List<Integer> nums) {
List<List<Integer>> result = new ArrayList<>();
permuteHelper(0, nums, result);
return result;
} public static void main(String[] args) {
Permutations p = new Permutations();
List<List<Integer>> result = p.permute(Arrays.asList(1, 2, 3));
System.out.println(result);
}
}

Permutations LT46的更多相关文章

  1. Permutations II

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

  2. [LeetCode] Permutations II 全排列之二

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

  3. [LeetCode] Permutations 全排列

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

  4. POJ2369 Permutations(置换的周期)

    链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  5. Permutations

    Permutations Given a collection of distinct numbers, return all possible permutations. For example,[ ...

  6. 【leetcode】Permutations

    题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...

  7. [leetcode] 47. Permutations II

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

  8. Leetcode Permutations

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

  9. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

随机推荐

  1. JDK各个版本比较 JDK5~JDK10

    JDK1.5新特性: 1.自动装箱与拆箱: 2.枚举 3.静态导入,如:import staticjava.lang.System.out 4.可变参数(Varargs) 5.内省(Introspec ...

  2. jvm中堆和栈的区别

    1.前言.    其实jvm能优化的空间不多,最主要的是使用的共享内存不要超过默认的2g或者自己调的参数.但了解一下还是有点意思的,建议面试时还是要看,别学笔者裸奔. 2.区别.   网上说是有5点区 ...

  3. lua keynote2

    [lua keynote2] 1.Lua函数可以返回多个结果值,比如string.find,其返回匹配串"开始和结束的下标"(如果不存在匹配串返回nil). > s, e = ...

  4. WPF双向绑定

    需求: 思想批量保存数据. 思路: 看了一下MVVM.发现只需要实现前台和后台数据的同步即可.也就是前台的文本框内容变化时后台的对象的属性也要变化就可以了. 参考: http://www.cnblog ...

  5. commit 流程

    COMMIT是一个非常快的操作,当我们发布commit命令时,真正困难的动作已经完成,在数据库中已经执行了数据更改,所以已经完成了99%的任务,例如:下列操作已经产生: 1.在SGA(Buffer C ...

  6. ASP.NET 登录验证 ihttpmoudle

    问题: 1.iis版本不同(IIS7.0,应用程序池采用的是集成模式,换成经典模式才起作用.) 在 IIS 7 以下的版本中,应用以下配置: <system.web> <httpMo ...

  7. Javascript重点汇总

    LazyMan 实现LazyMan(什么是LazyMan?请自行google) function _LazyMan(_name) { var _this = this; _this.tasks = [ ...

  8. cdh5.13.1 升/降级SPARK2 (parcel安装的同理)

    下载相关的CSD包与parcel包.parcel包SHA 放置在相关位置. 注意:重启cloudera-scm-server 从parcel 里删除旧包,启用新包 csd目录里其它JAR包要删除

  9. rancher 2 webhook 格式

    { "version":"4", "groupKey":<string>, "status":"& ...

  10. pta l2-1紧急救援(Dijkstra)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 题意:给n个城市,m条边,每个城市 ...