Leetcode 中等:78. 子集

题目:子集

  • 题号:78
  • 难度:中等
  • https://leetcode-cn.com/problems/subsets/

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

示例1:

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

示例2:

输入:nums = [0]
输出:
[
    [],
    [0]
]

实现

第一种:回溯法

依次以nums[i]为启始点进行搜索,且后续搜索数值都要大于前一个数值,这样会避免重复搜索。

回溯过程

C# 语言

public class Solution
{
    private IList<IList<int>> _result;
    
    public IList<IList<int>> Subsets(int[] nums)
    {
        _result = new List<IList<int>>();
        int len = nums.Length;

        if (len == 0)
        {
            return _result;
        }
        IList<int> item = new List<int>();
        Find(nums, 0, item);
        return _result;
    }

    private void Find(int[] nums, int begin, IList<int> item)
    {
        // 注意:这里要 new 一下
        _result.Add(new List<int>(item)); 

        if (begin == nums.Length)
            return;

        for (int i = begin; i < nums.Length; i++)
        {
            item.Add(nums[i]);
            Find(nums, i + 1, item);

            // 组合问题,状态在递归完成后要重置
            item.RemoveAt(item.Count - 1); 
        }
    }
}

第二种:子集扩展法

C# 语言

public class Solution
{
    public IList<IList<int>> Subsets(int[] nums)
    {
        IList<IList<int>> result = new List<IList<int>>();
        IList<int> item = new List<int>();
        result.Add(item);
        for (int i = 0; i < nums.Length; i++)
        {
            for (int j = 0, len = result.Count; j < len; j++)
            {
                item = new List<int>(result[j]);
                item.Add(nums[i]);
                result.Add(item);
            }
        }
        return result;
    }
}

第三种:位运算

思路: 利用整数集合的思路。

{1,2,3}为例,三个数,共2^3个子集。

000 -> []
100 -> [1]
101 -> [1,3]
110 -> [1,2]
111 -> [1,2,3]
...

C# 语言

public class Solution
{
    public IList<IList<int>> Subsets(int[] nums)
    {
        IList<IList<int>> result = new List<IList<int>>();
        int count = nums.Length;

        for (int i = 0; i < 1 << count; i++)
        {
            IList<int> item = new List<int>();
            for (int j = 0; j < count; j++)
            {
                int mask = 1 << j;
                if ((mask & i) != 0)
                    item.Add(nums[j]);
            }
            result.Add(item);
        }
        return result;
    }
}

Python 语言

class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        count = len(nums)
        result = []
        for i in range(1 << count):
            item = []
            for j in range(count):
                mask = 1 << j
                if (mask & i) != 0:
                    item.append(nums[j])
            result.append(item)
        return result
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青少年编程小助手_Python

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值