10-line c++ solution


  • 1
    M
    class Solution {
    public:
        static int comp(int a, int b)
        {
            return a>b;
        }
        
        vector<vector<int>> dfs(vector<int>& nums, int p) {
            vector<vector<int>> ans;
            if (p==nums.size()) {
                ans.push_back(vector<int> ());
            } else {
                for (auto i : dfs(nums, p+1)) {
                    ans.push_back(i);
                    i.push_back(nums[p]);
                    ans.push_back(i);
                }
            }
            return ans;
        }
        
        vector<vector<int>> subsets(vector<int>& nums) {
            sort(nums.begin(), nums.end(), comp);
            return dfs(nums, 0);
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.