如果你碰到排列组合的题,就想想是否可以用backtracking做,然后把模版记住:
private List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> subsets(int[] nums) { backtracking(nums, 0, new ArrayList<>(), res); return res; } private void backtracking(int[] nums, int index, List<Integer> list, List<List<Integer>> res){ res.add(new ArrayList(list)); for(int i=index;i<nums.length;i++){ list.add(nums[i]); backtracking(nums, i+1, list, res); list.remove(list.size()-1); } }