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 ...
随机推荐
- c++ String去除头尾空格
1.使用string的find_first_not_of,和find_last_not_of方法 #include <iostream> #include <string> s ...
- 谈谈Android NDK中动态链接库(.so文件)的优化
做了不少NDK相关的工作,不知道别人有没有同样的困惑,经常在编译C/C++代码的时候会出一些error或者warning,然后在网上搜,发现在Android.mk或者Application.mk文件中 ...
- js与ios桥接使用WebViewJavascriptBridge简单理解
https://github.com/marcuswestin/WebViewJavascriptBridge function setupWebViewJavascriptBridge(callba ...
- git恢复删除的分支及内容
git 删除分支git branch -D 分支名 git查看分支 git branch -a git 删除远程分支 git push origin :分支名 这里注意:git分支提交并且push了, ...
- go语言中文网中的资源
https://studygolang.com/subject/2 Go 系列教程 https://studygolang.com/subject/74 Go 语言机制 https://s ...
- centos7 安装keepalived
node1 192.168.5.101 node2 192.168.5.102 1.安装 openssl openssl-devel yum install openssl openssl-devel ...
- 基本select 语句总结
--------------基本select语句总结 8.6---------------------------------------------------------------------- ...
- 我的notepad++
我觉得,做开发的一定要有一个简单,但功能强大的文本编辑器.我比较喜欢notepad++,而且一直使用.准备通过这篇文章分享一下我的notepad++配置. 希望广大notepad++用户,如果有好的配 ...
- ios开发之--UIButton中imageView和titleLabel的位置调整
在使用UIButton时,有时候需要调整按钮内部的imageView和titleLabel的位置和尺寸.在默认情况下,按钮内部的imageView和titleLabel的显示效果是图片在左文字在右,然 ...
- 【数据分析】Superset 之三 Docker操作管理
一.进入容器 查看运行的容器:docker ps docker attach confident_thompson 或者 docker attach 34cd2299110f docker exec ...