给出一个数组,数组中的元素各不相同,找到该集合的所有子集(包括空集和本身)
举例说明:
int []nums={1,2,3} 
返回结果如下:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
使用回溯法解决上述问题。
 
相当于对一棵树的深度遍历操作。 

 
上述的输出结果还是有些规律的,也就是按照子集的元素个数来进行分解,元素个数为0,元素个数为1,元素个数为2,元素个数为3.

list.add(new ArrayList<>(tempList));
for(int i = startLen ; i < len ; i++){
tempList.add(nums[i]);
getSubset(list,tempList,i+1,nums,len);
tempList.remove(tempList.size()-1);
}
 
关键代码段如上所示。为了以后遇到这样的题目更顺手,把这道题举一个比较详细的计算过程
 
private static void getSubset(List<List<Integer>> list, List<Integer> tempList, int startLen, int[] nums, int len) 
 
上面为函数体。
其中第一个参数类型为List<List<Integer>> list用来保存所有的子集,作为最终的输出结果。
第二个参数为List<Integer> tempList用来记录某一个子集,
第三个参数为int startLen 用来标记开始的长度
第四个参数为int[] nums也就是最原始的集合
第五个参数为int len 表示数组nums的长度,可以避免在循环体中不断对数组nums进行求取长度。

参考代码
package leetcode_100;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /***
*
* @author pengfei_zheng
* 求集合的所有子集
*/
public class Solution78 {
public static List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> list = new ArrayList<>();//record the final answer
List<Integer> tempList = new ArrayList<>();//record one of the subSet
Arrays.sort(nums);
int len = nums.length;//prevent calculating the length in the function
getSubset(list, tempList, 0, nums, len);//calling the backtrack function
return list;
} private static void getSubset(List<List<Integer>> list, List<Integer> tempList, int startLen, int[] nums, int len) {
list.add(new ArrayList<>(tempList));//by calling itself to add tempList to the list
for(int i = startLen ; i < len ; i++){
tempList.add(nums[i]);// add element to tempList
getSubset(list,tempList,i+1,nums,len);//calling itself
tempList.remove(tempList.size()-1);//backtrack and remove the top element in tempList
}
}
public static void main(String[]args){
int []nums = {0,1,2,3};
List<List<Integer>> list = subsets(nums);
int len = list.size();
for(int i = 0 ; i < len; i++){
System.out.println(list.get(i));
}
} }

LeetCode 78 Subsets (所有子集)的更多相关文章

  1. [leetcode]78. Subsets数组子集

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

  2. leetcode 78. Subsets 、90. Subsets II

    第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...

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

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

  4. [LeetCode] 78. Subsets 子集合

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

  5. [LeetCode] 90. Subsets II 子集合之二

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

  6. Leetcode#78 Subsets

    原题地址 有两种方法: 1. 对于序列S,其子集可以对应为一个二进制数,每一位对应集合中的某个数字,0代表不选,1代表选,比如S={1,2,3},则子集合就是3bit的所有二进制数. 所以,照着二进制 ...

  7. 78 Subsets(求子集Medium)

    题目意思:求解一个数组的所有子集,子集内的元素增序排列eg:[1,3,2] result:[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]思路:这是一个递推的过程 [] ...

  8. LeetCode 78. Subsets(子集合)

    Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...

  9. [LeetCode] 78. Subsets tag: backtracking

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

随机推荐

  1. Web APi之HttpClient注意事项以及建议

    Web APi之HttpClient注意事项以及建议 前言 之前对于用SelfHost来手动实现Web API的宿主模式,似乎不是太深入,所以本篇文章我们一起来讨论关于利用HttpClient来访问W ...

  2. 编译libjpeg

    本来以为编译libjpeg很容易,结果弄了半天. 先百度了下看下教程,一般是设置path,这里我也做了 我的电脑  -> 属性 -> 高级  -> 环境变量   ,添加环境变量PAT ...

  3. keep-alive pipeline区别

    在http1.1中,引入了一种新的特性,即pipeline.那么什么是pipeline呢?pipeline其实就是流水线作业,它可以看作为keepalive的一种升华,因为pipeline也是基于长连 ...

  4. C++ 著名程序库 概览

          本文转载自: http://ace.acejoy.com/thread-3777-1-1.html   1.C++各大有名库的介绍--C++标准库 2.C++各大有名库的介绍--准标准库B ...

  5. C语言的基本数据类型长度

    PS:以下内容是在Xcode的编辑器64位环境下的测试结果,网上有关于64位和32位各数据类型存在的差异,请自行online search. main.m #import <Foundation ...

  6. python06 深浅拷贝原理

    preface 这里主要说深浅拷贝的原理.首先说说数字与字符串作为内存对象的重用,请看代码: a1=12345 b1=12345 a2=a1 print(id(a1),id(b1),id(a2)) 打 ...

  7. level1 -- unit 3 - 频率副词

    频率副词 never sometimes usually always never 从不 sometimes 有时 usually 通常 always 总是 频率排名(从最不到最经常): never ...

  8. Installing the Eclipse Plugin for ADT

    http://developer.android.com/sdk/installing/installing-adt.html   Download the ADT Plugin Start Ecli ...

  9. easyui分页,编辑datagrid某条数据保存以后跳转到某一页

    参考资料:http://caizhilin2010.iteye.com/blog/1731698 问题:商品列表页面采用easyui的datagrid展示数据,编辑某行数据保存以后,要求跳转到 用户在 ...

  10. Vim 操作符命令和动作命令

    http://www.cnblogs.com/littlerabbit/articles/2349951.html