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 ...
随机推荐
- centos 6.x系统升级glibc库至2.15版本的快速解决办法
CentOS 6.x系统升级glibc库至2.15版本的快速解决办法 1.先确保相关软件包已经安装 yum install -y glibc yum install -y glibc-common ...
- VSC KeyNote
[VSC KeyNote] 1.前后跳转. Alt + LeftArrow, Alt + RightArrow 2.缩进问题. vsc默认缩进为4,但js代码里缩进依旧是2. 因为vscode默认启用 ...
- 判断元素16种方法expected_conditions
前言 标签(空格分隔): 判断元素 经常有小伙伴问,如何判断一个元素是否存在,如何判断alert弹窗出来了,如何判断动态的元素等等一系列的判断,在selenium的expected_condition ...
- OpenSessionInViewFilter 的配置及作用(原文地址: http://blog.csdn.net/sunsea08/article/details/4545186)
spring为我们解决hibernate的Session的关闭与开启问题. Hibernate 允许对关联对象.属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Sessio ...
- 手机端用swiper组件 轮播图设置后右侧出现空白 及 部分手机浏览器打开网页空白
我的方法是设置内容css overflow:hidden;width:100%; ok. 之前搜到一个方法也可以,就是设置css height: auto;overflow-y: scroll; 但是 ...
- JSON Extractor
JMeter处理responses 的json 对于请求1返回的结果,处理以后作为请求2的参数,JMeter提供了JSON 提取器 比如 responses 返回: {"statusCode ...
- Disruptor框架EventProcessor和Workpool的使用
场景使用: 在HelloWorld的实例中,我们创建Disruptor实例,然后调用getRingBuffer方法去获取RingBuffer,其实在很多时候,我们可以直接使用RingBuffer,以及 ...
- 使用IntelliJ IDEA 搭建 spring mvc开发环境
填好GroupId.ArtifactId,一路Next创建工程,在main 目录创建 java文件夹并转换为Sources Root,创建好工程目录结构如下: 配置工程 配置 pom.xml,引入相关 ...
- swift - 解析三方 - ObjectMapper
// // JYQueryBespeakModel.swift // rtb // // Created by chen on 2018/3/30 // 查询预约信息 import UIKit imp ...
- C++ 计算直线的交点数(动态规划)
问题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1466 Problem Description 平面上有n条直线,且无三线共点,问这些直线能有多少种不同 ...