zoukankan      html  css  js  c++  java
  • 78. Subsets

    Given a set of distinct integers, nums, return all possible subsets (the power set).

    Note: The solution set must not contain duplicate subsets.

    Example:

    Input: nums = [1,2,3]
    Output:
    [
      [3],
      [1],
      [2],
      [1,2,3],
      [1,3],
      [2,3],
      [1,2],
      []
    ]

    AC code:

    class Solution {
    public:
        vector<vector<int>> subsets(vector<int>& nums) {
            vector<vector<int>> res;
            vector<int> temp;
            int len = nums.size();
            solve_subset(0, len, res, temp, nums);
            return res;
        }
        void solve_subset(int begin, int n, vector<vector<int>>& res, vector<int>& temp, vector<int>& nums) {
            res.push_back(temp);
            for (int i = begin; i < n; ++i) {
                temp.push_back(nums[i]);
                solve_subset(i+1, n, res, temp, nums);
                temp.pop_back();
            }
        }
    };
    

    Runtime: 4 ms, faster than 100.00% of C++ online submissions for Subsets.

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    codeforces 1096 题解
    pkuwc 前的任务计划
    codeforces 1093 题解
    luoguP5068 [Ynoi2015]我回来了
    luoguP5074 Eat the Trees
    二分
    保护
    数数字
    旅行
    すすめ!
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9842787.html
Copyright © 2011-2022 走看看