马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> arr = new ArrayList<>();
List<Integer> a = new ArrayList<>();
helper(nums,arr,a);
return arr;
}
public void helper(int[] nums,List<List<Integer>> arr,List<Integer> a){
if(a.size() == nums.length){
arr.add(new ArrayList<>(a));
return;
}
for(int i =0 ; i< nums.length; i++){
if(a.contains(nums[i])) continue;
a.add(nums[i]);
helper(nums,arr,a);
a.remove(a.size()-1);
}
}
}
|