子集

78. 子集

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

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

示例:

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

import java.util.ArrayList;
import java.util.List;

public class Seventy_eight {
    /**
     * 法一:递归+回溯法
     */
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> list = new ArrayList<Integer>();
        subsets(res, list, nums, 0);
        return res;
    }

    public void subsets(List<List<Integer>> res, List<Integer> temp, int[] nums, int start) {
        res.add(new ArrayList<>(temp));
        if (start == nums.length)
            return;
        for (int i = start; i < nums.length; i++) {
            temp.add(nums[i]);
            subsets(res, temp, nums, i + 1);
            temp.remove(temp.size() - 1);
        }
    }


    /**
     * 法二:位运算
     */
    public List<List<Integer>> subsets2(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        int end = (int) Math.pow(2, nums.length);
        for (int i = 0; i < end; i++)
            ans.add(helper(nums, i));
        return ans;
    }

    //把n化为二进制,0表示不取,1表示取
    public List<Integer> helper(int[] nums, int n) {
        List<Integer> ans = new ArrayList<>();
        int i = 0;
        while (n != 0) {
            if ((n & 1) == 1)//判断二进制最后一位是否为1
                ans.add(nums[i]);
            i++;
            n >>= 1;//右移1位去除最后1位二进制
        }
        return ans;
    }
}
class Solution {
public:
    vector<vector<int>> subsets(vector<int> &nums) {
        if (nums.empty())
            return vector<vector<int>>();
        vector<int> temp;
        vector<vector<int>> ans;
        helper(nums, ans, temp, 0);
        return ans;
    }

    void helper(vector<int> &nums, vector<vector<int>> &ans, vector<int> &temp, int start) {
        ans.push_back(temp);
        if (start == nums.size())
            return;
        for (int i = start; i < nums.size(); i++) {
            temp.push_back(nums[i]);
            helper(nums, ans, temp, i + 1);
            temp.pop_back();
        }
    }
};

90. 子集 II

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

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

示例:

输入: [1,2,2]
输出:
[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

package gongel_first;

import java.util.*;

public class Ninety {
    /**
     * 递归+回溯+去重
     */
    public void dfs(int[] nums, int start, List<Integer> temp, List<List<Integer>> ans) {
        if (!ans.contains(temp))
            ans.add(new ArrayList<>(temp));
        for (int i = start; i < nums.length; i++) {
            temp.add(nums[i]);
            dfs(nums, i + 1, temp, ans);
            temp.remove(temp.size() - 1);
        }
    }

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        if (nums == null || nums.length == 0)
            return new ArrayList<>();
        Arrays.sort(nums);
        List<Integer> temp = new ArrayList<>();
        List<List<Integer>> ans = new ArrayList<>();
        dfs(nums, 0, temp, ans);
        return ans;
    }
}
class Solution {
public:
    vector<vector<int>> subsetsWithDup(vector<int> &nums) {
        if (nums.empty())
            return vector<vector<int>>();
        vector<int> temp;
        set<vector<int>> ans;
        sort(nums.begin(), nums.end());
        helper(nums, ans, temp, 0);
        return vector<vector<int>>(ans.begin(), ans.end());
    }

    void helper(vector<int> &nums, set<vector<int>> &ans, vector<int> &temp, int start) {
        ans.insert(temp);
        if (start == nums.size())
            return;
        for (int i = start; i < nums.size(); i++) {
            temp.push_back(nums[i]);
            helper(nums, ans, temp, i + 1);
            temp.pop_back();
        }
    }
};

 

0