问题描述:

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. ralink雷灵RT5370 for mac ox 10.8 驱动

    博主2013年一月份安装了黑苹果发现 1000BGN 这斯全球无解. 在多米网上买了个 ralink5370 芯片的网卡.结果好多时候,总是闪退纠结ing 最后找到在官网上找到一个驱动. 目测网速还可 ...

  2. Tornado源码浅析

    初识tornado 经典的hello world 案例: import tornado.ioloop import tornado.web class MainHandler(tornado.web. ...

  3. php 问题及原因总结

    1.php 加水印时出现问题的原因 :或许某个参数输入错误,导致页面一点反应都没有.

  4. 处理界面上使用两个jq的报错

    转载:http://www.365mini.com/page/jquery_noconflict.htm <script src="jquery-1.9.1.js">& ...

  5. Django框架错误处理

    错误处理 在一些网站开发中.经常会需要捕获一些错误,然后将这些错误返回比较优美的界面,或者是将这个错误的请求做一些日志保存.那么我们本节就来讲讲如何实现. 常用的错误码: 404:服务器没有指定的ur ...

  6. 上手Keras

    Keras的核心数据是“模型”,模型是一种组织网络层的方式.Keras中主要的模型是Sequential模型,Sequential是一系列网络层按顺序构成的栈. Sequential模型如下: fro ...

  7. linux c编程:记录锁

    记录锁相当于线程同步中读写锁的一种扩展类型,可以用来对有亲缘或无亲缘关系的进程进行文件读与写的同步,通过fcntl函数来执行上锁操作.尽管读写锁也可以通过在共享内存区来进行进程的同步,但是fcntl记 ...

  8. Hurst指数以及MF-DFA

    转:https://uqer.io/home/ https://uqer.io/community/share/564c3bc2f9f06c4446b48393 写在前面 9月的时候说想把arch包加 ...

  9. POS杂项数据SAP记账程序

    *&---------------------------------------------------------------------* *& Report ZDQFI_904 ...

  10. python16_day03【集合、编码、函数、递归、内置函数】

    一.集合 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 #创建: s = {3,5,9,10} # ...