My simple iterative solution in C++


  • 0
    J
    class Solution {
    public:
        vector<vector<int> > subsets(vector<int> &S) {
            int n = S.size();
            vector<vector<int> > result;
            vector<int> vv;
            result.push_back(vv);
            sort(S.begin(), S.end());
            for(int i = 0; i < n; i++)
            {
                vector<vector<int> > temp;
                for(int j = 0; j < result.size(); j++)
                {
                    temp.push_back(result[j]);
                    result[j].push_back(S[i]);
                    temp.push_back(result[j]);
                }
                result = temp;
            }
            return result;
        }
    };

Log in to reply
 

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