思路:
递归回溯。典型应用
public class Solution {
int n;
List<List<Integer>> result;
List<Integer> temp;
boolean isUsed[];
int[]nums;
public List<List<Integer>> permute(int[] nums) {
n=nums.length;
isUsed=new boolean[n];
temp=new LinkedList<Integer>();
result=new LinkedList<List<Integer>>();
this.nums=nums;
help(0);
return result;
}
public void help(int step)
{
if(step==n)
{
result.add(new LinkedList<Integer>(temp));
return;
}
for(int i= 0;i<n;i++)
{
if(!isUsed[i])
{
isUsed[i]=true;
temp.add(nums[i]);
help(step+1);
isUsed[i]=false;
temp.remove(temp.size()-1);
}
}
}
}
本文介绍了一种使用递归回溯实现排列组合问题的解决方案。通过递归调用自身来寻找所有可能的答案,并利用一个布尔数组跟踪哪些元素已被使用。此方法适用于寻找所有可能的排列组合情况。
6243

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



