leetcode78 子集

给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

说明:解集不能包含重复的子集。

示例:

输入: nums = [1,2,3]
输出:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

思路:简单搜索,思路见代码。

class Solution {
    List<List<Integer>> lists = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
        if(nums == null || nums.length ==0)return lists;

        List<Integer> list = new ArrayList<>();
        process(list, nums, 0);
        return lists;
    }

    private void process(List<Integer>list, int[] nums, int start){
        lists.add(new ArrayList(list));
        for(int i = start; i < nums.length; i++){
            list.add(nums[i]);
            process(list, nums, i+1);
            list.remove(list.size()-1);
        }
    }
}
发布了412 篇原创文章 · 获赞 5125 · 访问量 84万+

猜你喜欢

转载自blog.csdn.net/hebtu666/article/details/103563754
今日推荐