子集 (Subsets)

 

思路:

// @Title: 子集 (Subsets)
// @Author: qisiii
// @Date: 2024-09-20 00:18:19
// @Runtime: 0 ms
// @Memory: 41.8 MB
// @comment: 
// @flag: 
class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        dfs(result, new ArrayList<>(), nums, 0);
        return result;
    }

    private void dfs(List<List<Integer>> result, List<Integer> list, int[] nums, int start) {
        result.add(new ArrayList<>(list));
        if (start == nums.length) {
            return;
        }
        for (int i = start; i < nums.length; i++) {
            list.add(nums[i]);
            dfs(result, list, nums, i + 1);
            list.remove(list.size() - 1);
        }
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18