题目:
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 思路二
建立一棵树,比如说:
方法二:
参考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;
}
}
}
本文详细阐述了使用递归和迭代方法解决全排列问题的算法分析及实现细节,包括建立树型结构和维护used数组以避免重复。通过AC代码展示了具体实现过程。
591

被折叠的 条评论
为什么被折叠?



