题目

类型:回溯
image.png

解题思路

https://leetcode-cn.com/problems/subsets/solution/c-zong-jie-liao-hui-su-wen-ti-lei-xing-dai-ni-gao-/

代码

  1. class Solution {
  2. public List<List<Integer>> subsets(int[] nums) {
  3. List<List<Integer>> res = new ArrayList<>();
  4. backtrack(0, nums, res, new ArrayList<Integer>());
  5. return res;
  6. }
  7. private void backtrack(int i, int[] nums, List<List<Integer>> res, ArrayList<Integer> tmp) {
  8. res.add(new ArrayList<>(tmp));
  9. for (int j = i; j < nums.length; j++) {
  10. tmp.add(nums[j]);
  11. backtrack(j + 1, nums, res, tmp);
  12. tmp.remove(tmp.size() - 1);
  13. }
  14. }
  15. }