leetcode -day31 Subsets I II

1、



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> >*  v;
    vector<vector<int> > subsets(vector<int> &S) {

        v = new vector<vector<int> >();
        //先排序
        sort(S.begin(),S.end());

        vector<int> res;
        generate(res, S, 0);
        return *v;
    }
    //对每个元素有放与不放两种选择
    void generate(vector<int> res, vector<int> &S, int i)
    {
        if(i == S.size())
        {
            v->push_back(res);
            return;
        }
        else
        {
            generate(res, S, i+1);//不放当前元素
            res.push_back(S[i]); //放入当前元素
            generate(res, S, i+1);
        }
    }
};

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

分析:此题和上题类似,就是有了反复元素,想法也是先进行排序,排序后,从头到尾遍历,记录每一个元素的个数,每一个子集中有0-i个指定元素(一共i个),代码例如以下:

class Solution {
public:
    vector<vector<int> >*  v;
    vector<vector<int> > subsetsWithDup(vector<int> &S) {

        v = new vector<vector<int> >();
        //先排序
        sort(S.begin(),S.end());

        vector<int> res;
        generate(res, S, 0,0,0);
        return *v;
    }
    //pre: 排序后前一个元素 num: 前一个元素出现的次数
    void generate(vector<int> res, vector<int> &S, int i,int pre,int num)
    {
        if(i == S.size())
        {
            v->push_back(res);
            for(int j=1; j<=num; ++j){
                res.push_back(pre); //放入之前元素
                v->push_back(res);
            }
            return;
        }
        else if(pre != S[i] || num < 1 ) //与之前元素不同或者是首次
        {
            if(num < 1){
                generate(res,S,i+1,S[i],1);
            }else{
                generate(res, S, i+1,S[i],1);//放入0个元素
                for(int j=1; j<=num; ++j){
                    res.push_back(pre);
                    generate(res, S, i+1,S[i],1);//放入i个元素后从当前位置開始
                }
            }
        }else{
            pre = S[i];
            generate(res, S, i+1,pre,num+1);
        }
    }
};

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-10-25 17:06:08

leetcode -day31 Subsets I 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:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

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——Pascal&#39;s Triangle II

Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Integer>> list = new ArrayList<List&