LeetCode 78 Subsets (所有子集)
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
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)

参考代码
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 (所有子集)的更多相关文章
- [leetcode]78. Subsets数组子集
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- leetCode 78.Subsets (子集) 解题思路和方法
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- [LeetCode] 78. Subsets 子集合
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- [LeetCode] 90. Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- Leetcode#78 Subsets
原题地址 有两种方法: 1. 对于序列S,其子集可以对应为一个二进制数,每一位对应集合中的某个数字,0代表不选,1代表选,比如S={1,2,3},则子集合就是3bit的所有二进制数. 所以,照着二进制 ...
- 78 Subsets(求子集Medium)
题目意思:求解一个数组的所有子集,子集内的元素增序排列eg:[1,3,2] result:[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]思路:这是一个递推的过程 [] ...
- LeetCode 78. Subsets(子集合)
Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...
- [LeetCode] 78. Subsets tag: backtracking
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
随机推荐
- linux下时间同步的两种方法分享
方法1:与一个已知的时间服务器同步 复制代码 代码如下: ntpdate time.nist.gov 其中 time.nist.gov 是一个时间服务器. 删除本地时间并设置时区为上海 复制代码 代码 ...
- [mysql-Ver5.6.23] windows版my.ini配置
基于utf8mb4比utf8多了种编码,能更好的支持emoji表情(http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.htm ...
- UITextField禁用掉编辑之后...
某些UITextField只为了摆数据,还有响应点击,为了避免频繁出现键盘,所以把UITextField的人机交互(userInteractionEnabled)给关闭了 此时,给UITextFiel ...
- Linux下修改MySql的root密码
linux下如何修改Mysql的root密码 今天,忘了mysql下的root密码,想重置一下,但找了多个网站上的方法均有问题,最后参考几家的过程,经过不断尝试获得,终于成功了,下面特将过程分 ...
- 时间戳Id
ID:格式据1970.1.1毫秒数 1535091029740 13位 问题:高并发,分布式明显会有问题,网上有雪花算法,但是位数跟我需要的不一样,暂时不考虑,以后研究再用. 参考: https:/ ...
- CSS3制作美丽的3D表单
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- 第四章 TCP粘包/拆包问题的解决之道---4.2--- 未考虑TCP粘包导致功能异常案例
4.2 未考虑TCP粘包导致功能异常案例 如果代码没有考虑粘包/拆包问题,往往会出现解码错位或者错误,导致程序不能正常工作. 4.2.1 TimeServer 的改造 Class : TimeServ ...
- MongoDB管理
前几篇文章都是从开发和使用的角度了解了MongoDB的各个知识点,这篇文章将从MongoDB管理的角度入手,了解MongoDB管理所要了解的基本知识. 数据库命令 在前面几篇文章中,已经接触了一些数据 ...
- Python easyGUI 猜数字
import easygui as g import random d=random.randint(0,10) while 1: g.msgbox("现在开始猜数字小游戏:") ...
- my97date 时间范围限制
需求:根据开始时间,动态限制结束时间 实现: <!DOCTYPE html> <html lang="en"> <head> <meta ...