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);
}
}
};

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

leetcode -day31 Subsets I II的更多相关文章

  1. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  2. [leetcode]90. Subsets II数组子集(有重)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  3. Java for LeetCode 090 Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  4. [LeetCode] 90. Subsets II 子集合 II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  5. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  6. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  7. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  8. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  9. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

随机推荐

  1. 最艰难的采访IT公司ThoughtWorks代码挑战——FizzBuzzWhizz游戏

    最近的互联网招聘平台拉勾网在五月推出了"最艰难的采访IT公司"码挑战活动,评选出了5个最难面试的IT公司,即:ThoughtWorks.Google.Unisys.Rackspac ...

  2. java 遍历树节点 同时保留所有的从根到叶节点的路径

    直接在代码.稍后细说 数据结构定义: /** * */ package Servlet; import java.util.ArrayList; import java.util.List; /** ...

  3. lodoop打印控制具体解释

    注意:这就需要引进的打印控制(我上传Demo同时): install_lodop32.exe install_lodop64.exe LodopFuncs.js jquery-1.10.0.min.j ...

  4. Linux 编程学习笔记----命令行参数处理

    转载请注明出处.http://blog.csdn.net/suool/article/details/38089001 问题引入----命令行參数及解析 在使用linux时,与windows最大的不同 ...

  5. Forbidden You don't have permission to access / on this server.

    原文:Forbidden You don't have permission to access / on this server. Forbidden You don't have permissi ...

  6. atitit.提升稳定性---hibernate 添加重试retry 机制解决数据库连接关闭

    atitit.提升稳定性---hibernate 添加重试retry 机制解决数据库连接关闭 1. 流程总结 retry(5times).invoke(xxx).test().rest().$() t ...

  7. linux内核包转发过程(三)NIC帧接收分析

    [版权声明:转载请保留源:blog.csdn.net/gentleliu.邮箱:shallnew*163.com] 每一个cpu都有队列来处理接收到的帧.都有其数据结构来处理入口和出口流量,因此.不同 ...

  8. 比ORA-24777: 我不使用不可移植数据库链接更郁闷的事情达成一致

    现场有一个同步误差,内容如下面:    java.sql.BatchUpdateException: ORA-24777: 不同意使用不可移植的数据库链路    at oracle.jdbc.driv ...

  9. Asp.net .net(C#) 获取当前命名空间,类名,方法名的方法

    public static string GetMethodInfo() {     string str = "";      //取得当前方法命名空间     str += & ...

  10. 网络资源(1) - Hadoop视频

    2014_08_23: hadoop03c_分布式文件系统HDFS http://v.youku.com/v_show/id_XNDgwNjg1OTY0.html?f=18604686 2014_08 ...