Open Shawngbk opened 7 years ago
public class Solution { public List<List> permute(int[] nums) { List<List> res = new ArrayList<List>(); List list = new ArrayList(); helper(res, list, nums); return res; }
private void helper(List<List<Integer>> res, List<Integer> list, int[] nums) { for(int i = 0; i < nums.length; i++) { if(!list.contains(nums[i])) { list.add(nums[i]); if(list.size() == nums.length) { res.add(new ArrayList<Integer>(list)); } else { helper(res, list, nums); } //每次删掉最后一个元素,能让每一个元素都出现在该位置上backtracking list.remove(list.size()-1); } } }
}
LinkedIn Microsoft
public class Solution { public List<List> permute(int[] nums) {
List<List> res = new ArrayList<List>();
List list = new ArrayList();
helper(res, list, nums);
return res;
}
}