Given a set of distinct integers, 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,3], a solution is:

求所有的子集合的问题,只不过这里的子集合里面的数字不会发生重复的,实际上比另一个会重复的还要简单一点,也是用dfs就可以解决,我还是喜欢将传递的变量生命成为private,这样方便一点,避免出现很长的参数列表:

 class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
sort(nums.begin(), nums.end());
rawVec = nums;
tmp.clear();
ret.clear();
dfs();
return ret;
} void dfs(int start)
{
ret.push_back(tmp);
if(start >= raw.size()) return;
if(start < rawVec.size()){
for(int i = start + ; i < rawVec.size(); ++i){
tmp.push_back(i);
dfs(i);
tmp.pop_back();
}
}
}
private:
vector<vector<int>> ret;
vector<int> tmp;
vector<int> rawVec;
};

java版本的代码如下所示,基本上去除了所有的全局变量,和Subsets II的java代码基本上是相同的:

 public class Solution {
public List<List<Integer>> subsets(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> ret = new ArrayList<List<Integer>>();
List<Integer>tmp = new ArrayList<Integer>();
dfs(ret, tmp, 0, nums, nums.length);
return ret;
} public void dfs(List<List<Integer>> ret, List<Integer> tmp, int start, int [] nums, int limit){
if(start > limit)
return;
else if(start == limit){
ret.add(new ArrayList(tmp));
}else{
ret.add(new ArrayList(tmp));
for(int i = start; i < limit; ++i){
tmp.add(nums[i]);
dfs(ret, tmp, i+1, nums, limit);
tmp.remove((Integer)nums[i]);
}
}
}
}

LeetCode OJ:Subsets(子集)的更多相关文章

  1. LeetCode OJ——Subsets

    http://oj.leetcode.com/problems/subsets/ 计算一个集合的子集,使用vector<vector<int> >,使用了进制的思想. #inc ...

  2. leetCode 78.Subsets (子集) 解题思路和方法

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

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

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

  4. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  5. [Leetcode 78]求子集 Subset

    [题目] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The ...

  6. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  7. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  8. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  9. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  10. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

随机推荐

  1. Inception 2.0

    文章<Rethinking the Inception Architecture for Computer Vision> 介绍 VGG与GoogLeNet相比更朴素,但计算量大.Goog ...

  2. HBA卡 和 RAID卡

    HBA卡: 只从HBA的英文解释HOST BUS ADAPTER(主机总线适配器)就能看出来,他肯定是给主机用的,一般HBA就是给主机插上后,给主机扩展出更多的接口,来连接外部的设备.大多数讲到HBA ...

  3. [Papers]Finding Advertising Keywords on Web Pages

    参考资料: Finding Advertising Keywords on Web Pages ,Wen-tau Yih,Joshua Goodman, Vitor R. Carvalho

  4. Linux下SPI测试程序

    /** 说明:SPI通讯实现* 方式一: 同时发送与接收实现函数: SPI_Transfer()* 方式二:发送与接收分开来实现* SPI_Write() 只发送* SPI_Read() 只接收* 两 ...

  5. 什么是JavaBeans?

    参看维基百科,归纳出以下几条: JavaBeans是指符合某些标准的类, Bean这个名称用于涵盖这个标准, 其目的在于创建可重用的Java组件. 由于Bean是很“死板”的东西,因此它可以持久存储, ...

  6. 跨平台移动开发_PhoneGap API 事件类型

    PhoneGap API Events backbuttondevicereadymenubuttonpauseresumeonlineofflinebatterycriticalbatterylow ...

  7. transition失效问题

    关于transition,css教程中有一个很简单的例子: <!DOCTYPE html> <html> <head> <meta charset=" ...

  8. 写时拷贝(Copy On Write)方案详解

    本文旨在通过对 写时拷贝 的四个方案(Copy On Write)分析,让大家明白写时拷贝的实现及原理. 关于浅拷贝与深拷贝,我在之前的博客中已经阐述过了  浅拷贝容易出现指针悬挂的问题,深拷贝效率低 ...

  9. Kubernetes busybox nslookup问题

    使用最新版本的busybox会出现nslookup提示无法解析的问题: Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find k ...

  10. 四月兄弟AprilBeacon

    硬件相关ibeacon https://www.aprbrother.com/