【LeetCode】Subsets (2 solutions)

Subsets

Given a set of distinct integers, S, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

For example,
If S = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

参照Subsets II的解法

解法一:

class Solution {
public:
    vector<vector<int> > subsets(vector<int> &S) {
        vector<vector<int> > result;
        int size = S.size();
        for(int i = 0; i < pow(2.0, size); i ++)
        {//2^size subsets
            vector<int> cur;
            int tag = i;
            for(int j = size-1; j >= 0; j --)
            {//for each subset, the binary presentation has size digits
                if(tag%2 == 1)
                    cur.push_back(S[j]);
                tag >>= 1;
                if(!tag)
                    break;
            }
            sort(cur.begin(), cur.end());
            result.push_back(cur);
        }
        return result;
    }
};

解法二:

class Solution {
public:
    vector<vector<int> > subsets(vector<int> &S) {
        vector<vector<int> > result;
        vector<int> cur;
        result.push_back(cur);  //empty set
        sort(S.begin(), S.end());
        for(int i = 0; i < S.size(); i ++)
        {
            int exist = result.size();
            for(int j = 0; j < exist; j ++)
            {
                cur = result[j];
                cur.push_back(S[i]);
                result.push_back(cur);
            }
        }
        return result;
    }
};

时间: 2024-10-18 01:48:59

【LeetCode】Subsets (2 solutions)的相关文章

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

【leetcode】Subsets (Medium) ☆

Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. 就是找出数字的全部子集. 我的思路: 设输入是 1  2  3  4 先把输入从小到大排序 长度 0 : [] 长度 1 : 把输入数据从第一

【leetcode】Subsets

Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1], [2], [1,

【leetcode】Subsets II

Subsets II Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,2], a so

【leetcode】Subsets II (middle) ☆

Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,2], a solution is:

【LeetCode】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

【LeetCode】Search Insert Position (2 solutions)

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread