问题描述:

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

Note: The solution set must not contain duplicate subsets.

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

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

算法分析:第一种方法循环遍历方法,每次在原有集合基础上添加新元素。第二种算法:该类问题属于Backtraking回溯算法。

public class SubSets
{
//循环遍历法
public ArrayList<ArrayList<Integer>> subsets(int[] s)
{
if(s == null)
{
return null;
} Arrays.sort(s); ArrayList<ArrayList<Integer>> res = new ArrayList<>(); for(int i = 0; i < s.length; i ++)
{
ArrayList<ArrayList<Integer>> temp = new ArrayList<>();
//get sets that are already in result
for (ArrayList<Integer> a : res)
{
temp.add(new ArrayList<Integer>(a));
}
//add S[i] to existing sets
for (ArrayList<Integer> a : temp)
{
a.add(s[i]);
}
//add S[i] only as a set
ArrayList<Integer> single = new ArrayList<>();
single.add(s[i]);
temp.add(single); res.addAll(temp);
}
//add empty set
res.add(new ArrayList<Integer>()); return res;
} //回溯法
public List<List<Integer>> subsets_backtaking(int[] nums)
{
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
dfs(res, new ArrayList<>(), nums, 0);
return res;
}
public void dfs(List<List<Integer>> res, List<Integer> temp, int[] nums, int start)
{
res.add(new ArrayList<>(temp));
for(int i = start; i < nums.length; i ++)
{
temp.add(nums[i]);
dfs(res, temp, nums, i + 1);
temp.remove(temp.size() - 1);
}
}
}

SubSets2:数组里有重复元素

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

Note: The solution set must not contain duplicate subsets.

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

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
public class SubSets2
{
//回溯法
public List<List<Integer>> subsets(int[] nums)
{
if(nums == null)
{
return null;
}
Arrays.sort(nums);//千万别忘了排序,否则就出错了,因为这个数组里面有重复元素
List<List<Integer>> res = new ArrayList<>();
dfs(res, new ArrayList<>(), nums, 0);
return res;
}
public void dfs(List<List<Integer>> res, List<Integer> temp, int[] nums, int start)
{
res.add(new ArrayList<>(temp));
for(int i = start; i < nums.length; i ++)
{
//skip duplicates
if(i > start && nums[i] == nums[i-1])
{
continue;
}
temp.add(nums[i]);
dfs(res, temp, nums, i + 1);
temp.remove(temp.size() - 1);
}
}
}
 
 

SubSets,SubSets2, 求数组所有子集的更多相关文章

  1. [Leetcode] subsets ii 求数组所有的子集

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  2. [Leetcode] subsets 求数组所有的子集

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

  3. [LeetCode] Find Peak Element 求数组的局部峰值

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  4. C++ 数组长度 以及 数组名作为参数传递给函数 以及 为什么不在子函数中求数组长度

    在看排序,首先是插入排序,思路理清后想用代码实现,然后问题来了: 如何求数组长度? 如果没记错,在Java中应该是有直接可用的方法的, Python中(序列)也有.len,在C/C++中,字符串倒是有 ...

  5. C#中求数组的子数组之和的最大值

    <编程之美>183页,问题2.14——求子数组的字数组之和的最大值.(整数数组) 我开始以为可以从数组中随意抽调元素组成子数组,于是就有了一种想法,把最大的元素抽出来,判断是大于0还是小于 ...

  6. 《github一天一道算法题》:分治法求数组最大连续子序列和

    看书.思考.写代码. /*************************************** * copyright@hustyangju * blog: http://blog.csdn. ...

  7. PHP使用array_intersect()函数求数组交集

    在PHP中求数组的交集,我们可以与PHP给我们提供的现成函数:array_intersect(),其用法格式为: array array_intersect(array array1,array ar ...

  8. 求数组的最小数、最大值,求一组数的平均数,sort函数详解,类数组转数组

    求数组的最小值和最大值 //求数组当中最大值和最小值 var arr=[3,2,6,1,45,23,456,23,2,6,3,45,37,89,30]; //第一种方法 根据排序方法来求最大值和最小值 ...

  9. C陷阱:求数组长度

    // 这是一篇导入进来的旧博客,可能有时效性问题. 程序中,当我们建立了一个int型数组:int a[]={1,2,3,4,5,6};随后我们可能需要知道它的长度,此时可以用这种方法:length = ...

随机推荐

  1. python框架Scrapy中crawlSpider的使用

    一.创建Scrapy工程 #scrapy startproject 工程名 scrapy startproject demo3 二.进入工程目录,根据爬虫模板生成爬虫文件 #scrapy genspi ...

  2. Storm-源码分析汇总

    Storm Features Storm 简介 Storm Topology的并发度 Storm - Guaranteeing message processing Storm - Transacti ...

  3. 东方通tongweb linux安装

    1.把安装的bin文件和license.dat文件放到/opt目录下 2.运行$sh Install_TW5.0.0.0_Standard_Linux.bin -i console 命令在 Linux ...

  4. JavaScript教程1

    1.什么是 JavaScript? JavaScript 是一门跨平台.面向对象的动态的弱类型的轻量级解释型语言,是一种基于对象和事件驱动并具有相对安全性的客户端脚本语言.应用于 HTML 文档能够在 ...

  5. 链路的有效性检测 及 基于TCP的通信为什么需要RETRY

    一.链路的有效性检测 当网络发生单通.连接被防火墙Hang住.长时间GC或者通信线程发生非预期异常时,会导致链路不可用且不易被及时发现. 特别是异常发生在凌晨业务低谷期间,当早晨业务高峰期到来时,由于 ...

  6. centos7 重启网卡失败

    今天在centOS 7 network服务重启不了 现把各种解决方法归纳整理,希望能让后面的同学少走点歪路... 首先看问题:执行service network restart命令后出现下面的错误: ...

  7. Hadoop MapReduce InputFormat基础

    有时候你可能想要用不同的方法从input data中读取数据.那么你就需要创建一个自己的InputFormat类.   InputFormat是一个只有两个函数的接口.   public interf ...

  8. yum安装mysql5.6

    1.检查系统是否安装其他版本的MYSQL数据 yum list installed | grep mysql yum -y remove mysql-libs.x86_64 2.安装及配置 wget ...

  9. knockout 学习使用笔记-----event绑定传参ko属性

    在绑定event的时候,需要传入ViewModal 本身的属性值(其实没必要,js直接能获取到,此处为测试相关参数的传递),如果不加(),会将绑定的function传进event(至于为嘛传了个fun ...

  10. s5_day9作业

    # 1 编写 tail -f a.txt |grep 'error' |grep '404'命令,周一默写 # import time # def tail(filepath,encoding='ut ...