给定一个数字列表,返回其所有可能的排列。
注意事项
你可以假设没有重复数字。
样例
给出一个列表[1,2,3]
,其全排列为:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
class Solution {
/**
* @param nums: A list of integers.
* @return: A list of permutations.
*/
public List<List<Integer>> permute(int[] nums) {
// write your code here
Arrays.sort(nums);
List<List<Integer>> result=new ArrayList<List<Integer>>();
List<Integer> tempList=new ArrayList<Integer>();
backTracking(nums, result, tempList);
return result;
}
private static void backTracking(int[]nums, List<List<Integer>> result, List<Integer> tempList){
if(tempList.size()==nums.length){
result.add(new ArrayList<Integer>(tempList));
}else{
for(int i=0;i<nums.length;i++){
if(tempList.contains(nums[i])){
continue;
}else{
tempList.add(nums[i]);
backTracking(nums, result, tempList);
tempList.remove(tempList.size()-1);
}
}
}
}
}