题目分析
回溯法,这道题理解的不够好,特此标记,后补。
代码
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null || nums.length == 0) return res;
helper(res, new ArrayList<>(), nums);
return res;
}
public static void helper(List<List<Integer>> res, List<Integer> list, int[] nums) {
if(list.size() == nums.length) {
res.add(new ArrayList<Integer>(list));
}
for(int i = 0; i < nums.length; i++) {
if(list.contains(nums[i])) continue;
list.add(nums[i]);
helper(res, list, nums);
list.remove(list.size() - 1);
}
}
}
网友评论