[leetcode] Subsets
来源:互联网 发布:线性映射及其矩阵表示 编辑:程序博客网 时间:2024/06/16 21:09
Subsets
Total Accepted: 37337 Total Submissions: 132727My SubmissionsGiven a set of distinct integers, S, 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 S = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]
package com.wyt.leetcodeOJ;import java.util.ArrayList;import java.util.Arrays;import java.util.List;/** * @author wangyitao * @Date 2015-01-08 * @version 1.0 * @AC Time 202ms */public class Subsets {public static void main(String[] args) {int[] S = {1,2};int max = 2>>1;System.out.println(max);System.out.println(7&1);List<List<Integer>> subsets = subsets(S);System.out.println(subsets);}public static List<List<Integer>> subsets(int[] S) { List<List<Integer>> subsets = new ArrayList<List<Integer>>(); if (S != null) { Arrays.sort(S);//子集有序,题目所需 int n = 1 << S.length;//子集个数为2^S.length for (int i = 0; i < n; i++) { List<Integer> list = new ArrayList<Integer>(); int index = 0; int j = i; while (j > 0) { if((j & 1) != 0) {//j的二进制形式,按位与1,为0表示该位置的元素没有如,101&1==1-->add(s0),index+1=s1,j>>1=10&1==0,s1不在, list.add(S[index]); } index++;//标志前移 j = j >> 1;//j右移,一直到0 } subsets.add(list); }} return subsets; }}
0 0
- 【LeetCode】 Subsets Subsets II
- 【LeetCode】Subsets & Subsets II
- LeetCode Subsets && Subsets II
- LeetCode Subsets && Subsets II
- LeetCode-Subsets & Subsets II
- LeetCode: Subsets
- LeetCode Subsets
- [Leetcode] Subsets
- LeetCode : Subsets
- [LeetCode] Subsets
- Leetcode: Subsets
- Leetcode: Subsets ||
- [leetcode] subsets
- Leetcode - Subsets
- [LeetCode]Subsets
- LeetCode-Subsets
- LeetCode - Subsets
- LeetCode | Subsets
- 数据结构排序算法
- ios支付宝集成各种血泪坑和第三方平台不支持64位的解决办法
- scala类型参数的问题
- Repeater嵌套绑定Repeater
- 求数组次大值
- [leetcode] Subsets
- PDF转word在线转换
- VS2010 libmingwex.lib 无法解析的外部符号__imp____lc_codepage 错误
- Android项目开发前准备工作(二)
- Android Service完全解析,关于服务你所需知道的一切(下)
- 细说HTML元素的隐藏和显示display+visiblity
- HTTP常见错误
- 《大型网站技术架构》总结---架构演化
- jquery ui autocomplete使用示例