Generate all possible subsets from a set of distinct integers.

This problem asks you to generate all possible subsets from a set of distinct integers. For example, given the input [1,2,3], the output should be [[3],[1 ],[2],[1,2,3],[1,3],[2,3],[1,2],[]]

Problem

Given a set of distinct integers, return all possible subsets.
Example input:
[1,2,3]
Example output:
[[3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[]]

Solution

This solution is in C++. Our A.I. can create solutions in multiple languages.
by kayakane
#include <iostream>
#include <vector>

using namespace std;

vector<vector<int>> subsets(vector<int>& nums) {
    vector<vector<int>> res;
    vector<int> cur;
    res.push_back(cur);
    for (int i = 0; i < nums.size(); i++) {
        int n = res.size();
        for (int j = 0; j < n; j++) {
            cur = res[j];
            cur.push_back(nums[i]);
            res.push_back(cur);
        }
    }
    return res;
}

int main() {
    vector<int> nums = {1, 2, 3};
    vector<vector<int>> res = subsets(nums);
    for (int i = 0; i < res.size(); i++) {
        for (int j = 0; j < res[i].size(); j++) {
            cout << res[i][j] << " ";
        }
        cout << end l;
    }
    return 0;
}

A.I. Evaluation of the Solution

The candidate's solution is complete and solves the problem. The candidate's approach is to first add the empty subset to the result, and then iterate through the numbers in the input array. For each number, the candidate loops through all the subsets in the result and adds the number to create a new subset. This approach is correct.

Evaluated at: 2022-11-28 04:15:52