leetcode 78. 子集

78. 子集

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/subsets
著作权归领扣网络所有。

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

示例:

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

方法一:迭代法实现子集枚举

时间复杂度:O(n x 2^n)
空间复杂度:O(n)

0/1 序列子集 0/10/1 序列对应的二进制数
000{}0
001{9}1
010{2}2
011{2, 9}3
100{ 5 }4
101{ 5, 9}5
110{ 5, 2}6
111{5,2,9}7
/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
    if (! nums) {
        *returnSize = 0;
        return NULL; 
    }

    int max = 1 << numsSize;
    int **res = (int **)calloc(sizeof(int*), max);
    *returnColumnSizes = (int *)calloc(sizeof(int), max);
    for (int i = 0; i < max; i++) {
        int j = 0;
        int k = 0;
        res[i] = (int *)calloc(sizeof(int), numsSize);
        while( j < numsSize ) {
            if ( i & (1 << j) ) {
                res[i][k++] = nums[j];
            }
            j++;
        }
        (*returnColumnSizes)[i] = k;
    }

    *returnSize = max;

    return res;
}

方法二:逐个枚举追加元素

逐个枚举,空集的幂集只有空集,每增加一个元素,让之前幂集中的每个集合,追加这个元素,就是新增的子集。

非递归写法:

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer[][]
     */
    function subsets($nums) {
        $res = [[]];
        for ($i = 0; $i < count($nums); $i++) {
           foreach($res as $k => $v) {
               $new = array_merge($v, [$nums[$i]]);
               array_push($res, $new);
           }
        }

        return $res;
    }
}

递归写法:

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer[][]
      class Solution {
     */
    function subsets($nums) {
        $res = [[]];
        $this->recursion($nums, 0, $res);

        return $res;
    }

    function recursion($nums, $index, &$res) {
        if ( $index < count($nums) ) {
            foreach ($res as $val) {
                $res[] = array_merge($val, (array)$nums[$index]);
            }
            $this->recursion($nums, $index+1, $res);
        }
    }
}

方法三:回溯法

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer[][]
     **/

    private $res = [];

    function subsets($nums) {
        $this->dfs($nums, 0, []);

        return $this->res;
    }

    function dfs($nums, $start, $path) {
        array_push($this->res, $path);
        for ($i = $start; $i < count($nums); $i++ ) {
            array_push($path, $nums[$i]);
            $this->dfs($nums, $i+1, $path);
            array_pop($path);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值