leetcode: Subsets & Subsets II

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


class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
sort(S.begin(),S.end());
vector<int> buf;
vector<vector<int> > res;
subset(S,0,buf,res);
return res;
}
private:
void subset(vector<int> &S, int start, vector<int> &buf, vector<vector<int> > &res) {
if(start == S.size()) {
res.push_back(buf);
return;
}

subset(S,start+1,buf,res);

buf.push_back(S[start]);
subset(S,start+1,buf,res);
buf.pop_back();
}
};

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 solution is:

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


class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
sort(S.begin(),S.end());
vector<int> buf;
vector<vector<int> > res;
subset(S,0,buf,res);
//res.push_back(vector<int> {});
return res;

}
private:
void subset(vector<int> &S, int start, vector<int> &buf, vector<vector<int> > &res) {
if(start == S.size()) {
res.push_back(buf);
return;
}
int nextStart = distance(S.begin(),upper_bound (S.begin()+start, S.end(), S[start]));
for(int i=0;i<(nextStart-start);i++) {
buf.push_back(S[start]);
}
for(int i=(nextStart-start);i>0;i--) {
subset(S,nextStart,buf,res);
buf.pop_back();
}
subset(S,nextStart,buf,res);
}
};

leetcode: Subsets & Subsets II,布布扣,bubuko.com

时间: 2024-10-27 11:19:20

leetcode: Subsets & Subsets II的相关文章

LeetCode --- 90. 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]

Java for LeetCode 090 Subsets II

Given a collection of integers that might contain duplicates, nums, 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 nums = [1,2,2], a soluti

【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]90. Subsets II数组子集(有重)

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 题意: 是的[leetcode

[LeetCode] 90.Subsets II tag: backtracking

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 这个题目思路

LeetCode OJ - Subsets 1 &amp;&amp; 2

这道题的做法,一定得掌握啊!!!  elegant & beautiful & concise 下面是AC代码: 1 /** 2 * Given a set of distinct integers, S, return all possible subsets. 3 * 这道题的做法应该要记住!!!!! 4 * @param s 5 * @return 6 */ 7 public ArrayList<ArrayList<Integer>> subsets(int[

【leetcode】N-queens II

问题: 返回N皇后问题解的个数. 分析: 详见 N-queens 实现: bool nextPermutation(vector<int> &num) { int i = num.size() - 1; while (i >= 1) { if(num[i] > num[i - 1]) { --i; int ii = num.size() - 1; while (ii > i && num[ii] <= num[i]) --ii; if(ii &g

[LeetCode] Jump Game II(贪婪算法)

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

leetCode: Single Number II [137]

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他