当前位置: 代码迷 >> 综合 >> leetcode(78)subset
  详细解决方案

leetcode(78)subset

热度:54   发布时间:2023-11-30 13:22:56.0

文章目录

  • leetcode(78)subset
    • 我的主页

leetcode(78)subset

public class Solution {
    public List<List<Integer>> subsets(int[] nums) {
    List<List<Integer>> results = new ArrayList<>();if (nums == null || nums.length == 0) {
    return results;}List<Integer> subset = new ArrayList<>();dfsHelper(nums, 0, results, subset);return results;}private void dfsHelper(int[] nums, int startIndex, List<List<Integer>> results, List<Integer> subset) {
    // deep copy and add to resultsresults.add(new ArrayList<>(subset));for (int i = startIndex; i < nums.length; i++) {
    subset.add(nums[i]);dfsHelper(nums, i + 1, results, subset);subset.remove(subset.size() - 1);}   }
}

我的主页