leetcode 46. Permutations
Given a collection of distinct integers, return all possible permutations.Example:
Input:
Output:
[
,
,
,
,
,
]
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)) continue;
a.add(nums);
helper(nums,arr,a);
a.remove(a.size()-1);
}
}
}
页:
[1]