给定一个没有重复数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
class Solution {
public static List<List<Integer>> ans = new ArrayList<List<Integer>>();
public static int[] path = new int[100];
public static boolean[] v = new boolean[100];
public static void robot(int idx,int[] nums) {
if(idx >= nums.length) {
List<Integer> tmp = new ArrayList<Integer>();
for(int i = 0;i<nums.length;++i) {
tmp.add(nums[path[i]]);
}
ans.add(tmp);
return;
}
for(int i=0;i<nums.length;++i)
if(v[i] == false)
{
path[idx] = i;
v[i] = true;
robot(idx +1,nums);
v[i] = false;
}
}
public List<List<Integer>> permute(int[] nums) {
ans.clear();
robot(0, nums);
return ans;
// TODO Auto-generated method stub
}
}