Permutations LT46
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的更多相关文章
- Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- POJ2369 Permutations(置换的周期)
链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- Permutations
Permutations Given a collection of distinct numbers, return all possible permutations. For example,[ ...
- 【leetcode】Permutations
题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- Leetcode Permutations
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 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 ...
随机推荐
- Shiro在Spring session管理
会话管理 在shiro里面可以发现所有的用户的会话信息都会由Shiro来进行控制,那么也就是说只要是与用户有关的一切的处理信息操作都可以通过Shiro取得,实际上可以取得的信息可以有用户名.主机名称等 ...
- Java中this和super的用法和区别
super(参数):调用父类中的某一个构造函数(应该为构造函数中的第一条语句). this(参数):调用本类中另一种形式的构造函数(应该为构造函数中的第一条语句). this的实例: package ...
- Avatar
[Avatar] 1.Retargeting of Humanoid animations Retargeting is only possible for humanoid models, wher ...
- Navicat的外键设置
“名”:可以不填,你一会保存成功系统会自动生成. “栏位”:这个子表哪个键设置为外键. “参考数据库”:外键关联的数据库. “参考表”:关联的父表 “参考栏位”:父表关联的的字段,一般是id “删除时 ...
- IMPDP NETWORK_LINK参数
在<[IMPDP]同一数据库实例不同用户间数据迁移复制—— NETWORK_LINK参数>(http://space.itpub.net/519536/viewspace-631571)文 ...
- xnconvert 图片转换工具
xnconvert是一款简单高效的图片转换工具.xnconvert能够批量地进行图片格式转换,并具有一定的图片处理功能,可以增加水印.特效,支持放大缩小.旋转等. xnconvert功能介绍: 你可以 ...
- oracle中获取当前整点和上一个小时整点,日期类型
select to_date(to_char(sysdate,'yyyy-mm-dd hh'),'yyyy-mm-dd hh:mi:ss') from dual;select to_date(to_c ...
- python颜色及背景
linux终端中的颜色是用转义序列控制的,转义序列是以ESC开头,可以用\033完成相同的工作(ESC的ASCII码用十进制表示就是27,等于用八进制表示的33). 书写格式,和相关说明如下: 1 2 ...
- tableView与导航栏的偏移问题
//方法1.关闭自动布局 self.automaticallyAdjustsScrollViewInsets = NO; //方法2.改变相对于scrollview的位置 //contentInset ...
- jdk与eclipse位数不一致出现的问题
今天运行eclipse时出现了如下问题: 后来去网上查了一下,如果jdk版本的位数和eclipse安装版本所需要的版本位数不相同,就会出现如图所示的问题,导致eclipse打不开. 1.查询jdk版本 ...