首页 > 其他分享 >78. Subsets

78. Subsets

时间:2022-11-13 22:00:48浏览次数:57  
标签:arr group Subsets temp int List result 78

样例
输入
{1,8,5,4}

输出

[[], [1], [1, 4], [1, 4, 5], [1, 4, 5, 8], [1, 4, 8], [1, 5], [1, 5, 8], [1, 8], [4], [4, 5], [4, 5, 8], [4, 8], [5], [5, 8], [8]]

public List<List<Integer>> allSubList(int[] arr){
List<Integer> temp = new ArrayList<>();
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(arr);
group(arr, temp, result, 0);
return result;
}

private void group(int[] arr, List<Integer> temp, List<List<Integer>> result, int index){
result.add(new ArrayList<>(temp));
if (temp.size() == arr.length)
return;
for(int i = index; i < arr.length; i++){
temp.add(arr[i]);
group(arr, temp, result,i+1);
temp.remove(temp.size()-1);
}
}

标签:arr,group,Subsets,temp,int,List,result,78
From: https://www.cnblogs.com/MarkLeeBYR/p/16887119.html

相关文章

  • [VP]Codeforces Round #678 (Div. 2)
    诈骗题专场A.Reorder题意:给你一个长度为\(n\)的数组\(a\),问是否可以把这个重新排序后,使得\[\sum\limits^{n}_{i=1}\sum\limits^{n}_{j=i}\dfrac{a_j}{j}=m\]解法:......
  • luogu P4786 [BalkanOI2018]Election
    题面传送门离谱题,结论出奇的简单。首先我们考虑\(O(nq)\)怎么做。显然所有C都要放在最终序列中,然后问题就变成往里面填T。我们考虑第一个T填在能填的最开始的位置上,因......
  • Codeforces Round #786 (Div. 3) 补题记录
    小结:A,B,F切,C没写1ll对照样例才发现,E,G对照样例过,D对照样例+看了其他人代码(主要急于看后面的题,能调出来的但偷懒了。CF1674ANumberTransformation考虑若\(y\)......
  • #10078. 「一本通 3.2 练习 4」新年好
     从1出发访问5个给定点,最小化路程 枚举5个点的排列,然后单源最短路#include<iostream>#include<cstring>#include<queue>usingnamespacestd;structT{......
  • 题解 P4778【Counting swaps】
    problem一次操作指随意选定\(x,y\)并交换\(a_x,a_y\),请问有多少种方案,能用最少的操作次数重排一个排列\(a\)?\(n\leq10^5,P=10^9+7\)。solution0连边\(i\toa_i\)......
  • [AcWing 789]数的范围
    点击查看代码#include<iostream>usingnamespacestd;constintN=100010;intn,m;intq[N];intmain(){cin>>n>>m;for(inti=0;i<n;i......
  • 洛谷B2078含k个三的数
    自行体会如果实在不会,就调试一下#include<stdio.h>intmain(){longintm;intn,k,num=0;scanf("%ld%d",&m,&k);for(inti=1;i<=15;i++){if(i=......
  • 洛谷-P3478 STA-Station
    STA-Station换根dp模板去到相邻的点可以根据去到的点的子树有多少个结点,来调整当前的值#include<iostream>#include<cstdio>#include<algorithm>#include<vecto......
  • POJ 2478 Farey Sequence
    DescriptionTheFareySequenceFnforanyintegernwithn>=2isthesetofirreduciblerationalnumbersa/bwith0<a<b<=nandgcd(a,b)=1arrang......
  • leetcode-1784-easy
    CheckifBinaryStringHasatMostOneSegmentofOnesGivenabinarystringswithoutleadingzeros,returntrueifscontainsatmostonecontiguoussegment......