标签:backtrac res list back int ati rac ons []
1 class Solution { 2 List<List<Integer>> res = new ArrayList<>(); 3 public List<List<Integer>> combine(int n, int k) { 4 if(k == 0) return null; 5 int[] nums = new int[n]; 6 for(int i = 0; i < n; i++) { 7 nums[i] = i+1; 8 } 9 backtrack(nums, k, new ArrayList<>(), 0); 10 return res; 11 12 } 13 14 public void backtrack(int[] nums, int k, List<Integer> list, int position) { 15 if(list.size() == k) { 16 res.add(new ArrayList<>(list)); 17 return; 18 } 19 for(int i = position; i < nums.length; i++) { 20 list.add(nums[i]); 21 backtrack(nums, k, list, i+1); 22 list.remove(list.size() - 1); 23 } 24 } 25 }
标签:backtrac res list back int ati rac ons []
原文地址:https://www.cnblogs.com/goPanama/p/9652696.html