[LeetCode][Java] Permutations
来源:互联网 发布:51单片机c语言教程pda 编辑:程序博客网 时间:2024/05/22 17:17
题目:
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]
.
题意:
给定一组数字,返回所有的可能的组合。
比如:
[1,2,3]有下面的组合:
[1,2,3]
, [1,3,2]
, [2,1,3]
, [2,3,1]
, [3,1,2]
, and [3,2,1]
.
算法分析:
方法一:
建立树,并进行递归 参考博客:http://blog.csdn.net/tuantuanls/article/details/8717262 思路二
建立一棵树,比如说:
对于第k层节点来说,就是交换固定了前面 k-1 位,然后分别 swap(k,k), swap(k, k+1) , swap(k, k+2) ...
例如上图中的第三层,固定了第一位(即2),然后分别交换第1,1位,1,2位,1,3位
方法二:
参考http://blog.csdn.net/linhuanmars/article/details/21569031
方法还是原来那个套路,还是用一个循环递归处理子问题。区别是这里并不是一直往后推进的,前面的数有可能放到后面,所以我们需要维护一个used数组来表示该元素是否已经在当前结果中,因为每次我们取一个元素放入结果,然后递归剩下的元素,所以不会出现重复。时间复杂度还是NP问题的指数量级。
AC代码:
方法一:
public class Solution { public ArrayList<ArrayList<Integer>> permute(int[] num) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); permute(num, 0, result); return result; } void permute(int[] num, int start, ArrayList<ArrayList<Integer>> result) { if (start >= num.length) //终止条件,递归到末尾节点是,将数组转化为链表 { ArrayList<Integer> item = convertArrayToList(num); result.add(item); } for (int j = start; j <= num.length - 1; j++) { swap(num, start, j);//交换 permute(num, start + 1, result);//交换后子代递归 swap(num, start, j);//恢复到交换前的初始状态,以便于得出下一次的交换结果 } } private ArrayList<Integer> convertArrayToList(int[] num) //数组变链表 { ArrayList<Integer> item = new ArrayList<Integer>(); for (int h = 0; h < num.length; h++) item.add(num[h]); return item; } private void swap(int[] a, int i, int j) //交换 { int temp = a[i]; a[i] = a[j]; a[j] = temp; }}
方法二:
public ArrayList<ArrayList<Integer>> permute(int[] num) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(num==null || num.length==0) return res; helper(num, new boolean[num.length], new ArrayList<Integer>(), res); return res;}private void helper(int[] num, boolean[] used, ArrayList<Integer> item, ArrayList<ArrayList<Integer>> res){ if(item.size() == num.length) { res.add(new ArrayList<Integer>(item)); return; } for(int i=0;i<num.length;i++) { if(!used[i]) { used[i] = true; item.add(num[i]); helper(num, used, item, res); item.remove(item.size()-1); used[i] = false; } }}
0 0
- [Leetcode] Permutations (Java)w
- [Leetcode] Permutations II (Java)
- [LeetCode][Java] Permutations
- [LeetCode][Java] Permutations II
- [leetcode-46]Permutations(java)
- (Java)LeetCode-46. Permutations
- [LeetCode-Java]46. Permutations
- 46.Permutations leetcode java
- [leetcode-47]Permutations II(java)
- (Java)LeetCode-47. Permutations II
- ?47. Permutations II leetcode java
- LeetCode- 46/47. Permutations/Permutations || (JAVA) (全排列1,2)
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- leetcode JAVA Permutations 难度系数3 3.14
- [leetcode]46. Permutations@Java解题报告
- SQLite数据库文件格式
- 前端不为人知的一面———前端冷知识集锦
- maven使用的总结
- Charles使用教程
- HTTP/1.1 500 Internal Server Error
- [LeetCode][Java] Permutations
- Spring配置中<bean>的id和name属性区分
- maven用途、核心概念、用法、常用参数和命令、扩展
- Xcode如何添加pch文件
- Android 自定义控件
- Java设计模式----工厂模式(一)
- STM32F030, 使用嘀嗒定时器Systick实现LED闪烁
- 程序员奇葩面试的奇葩问题
- Maven发布项目