Leetcode 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],
  []
]

题目链接:https://leetcode.com/problems/subsets/

这几天在学python,今天突然写,脑子突然卡克了,还是借鉴了我自己在Leetcode 77. Combinations部分全排列的思路,才搞定

class Solution {
public:
   void  fun(vector<vector<int>> &res,vector<int> nums,vector<int> tmp,int s, int k)
    {
        if(tmp.size()==k)
        {
            res.push_back(tmp);
        }
        else if(nums.size()-s+1>=k-tmp.size())
        {
            for(int i=s;i<nums.size();i++)
            {
                tmp.push_back(nums[i]);
                fun(res,nums,tmp,i+1,k);
                tmp.pop_back();
            }
        }
        
    }
           
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>> res;
        vector<int> tmp;
        for(int i=0;i<=nums.size();i++)
        {
           fun(res,nums,tmp,0,i); 
        }
        return res;
    }
};

 

上一篇:最短路问题1:Floyd算法及其应用1(模板与传递闭包问题)


下一篇:Floyd 判圈算法