【LeetCode OJ 078】Subsets
来源:互联网 发布:排序算法时间复杂度 编辑:程序博客网 时间:2024/06/07 00:01
题目链接:https://leetcode.com/problems/subsets/
题目:Given a set of distinct integers, nums, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- 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], []]解题思路:搜了一下网上的解题思路,有一个感觉很巧妙。大致的思路是这样的,一个数组的子集个数是这个数组的长度的n次方(2^n),例如a[2]={1,2},子集个数就是2^2=4,二进制表示为00,01,10,11,分别表示空集、{1}、{2}、{12},具体的实现代码如下:
public class Solution{public List<List<Integer>> subsetsWithDup(int[] nums) {List<List<Integer>> result=new ArrayList<List<Integer>>();//先对数组进行排序Arrays.sort(nums);int n=nums.length;//1<<n就是该数组子集的个数for(int i=0;i<(1<<n);i++){List<Integer> list=new ArrayList<Integer>();for(int j=0;j<n;j++){//(i&(1<<j))!=0表示数组中索引值为j的数在当前的子集中if((i&(1<<j))!=0){list.add(nums[j]);}}result.add(list);}return result; }}
0 0
- 【LeetCode OJ 078】Subsets
- LeetCode OJ:Subsets II
- LeetCode OJ:Subsets
- LeetCode OJ - Subsets
- LeetCode OJ - Subsets II
- LeetCode OJ Subsets
- LeetCode OJ Subsets II
- LeetCode OJ-78.Subsets
- 【LeetCode OJ 090】Subsets II
- Leetcode OJ 78 Subsets [medium]
- LeetCode OJ算法题(七十八):Subsets
- LeetCode OJ 之 Subsets (子集合)
- LeetCode OJ 系列之78 Subsets --Python
- <LeetCode OJ> 78 / 90 Subsets (I / II)
- LeetCode: Subsets [078]
- LeetCode 078 Subsets
- LeetCode(078) Subsets (Java)
- 078 Subsets [Leetcode]
- 添加jstatd、jmx启动远程java监控或tomcat监控
- OpenCV学习笔记(二):对opencv.hpp头文件的认识
- android自定义控件(二),简易的数值输入器
- iOS重绘机制drawRect
- cmake使用示例与整理总结
- 【LeetCode OJ 078】Subsets
- MyBatis中对List<Object> 对象List的批处理插入操作
- 做一个合格的程序猿之浅析Spring IoC源码(十一)Spring refresh()方法解析之一
- 阿里云centos搭建python
- Debian安装Arduino IDE
- 使用spring4添加调度任务task的namespace
- 【ITOO】--- 考评拼接表导出Excel
- C++开源库
- 迅雷云监工(Crysadm)