题解 | #集合的所有子集#

集合的所有子集

http://www.nowcoder.com/practice/c333d551eb6243e0b4d92e37a06fbfc9

import java.util.*;

public class Solution {
    public static ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
    public ArrayList<ArrayList<Integer>> subsets(int[] S) {
        ArrayList<Integer> track = new ArrayList<>();
        Arrays.sort(S);
        dfs(S, 0, track);
        Collections.sort(res,(o1,o2)->{
            return o1.size()-o2.size();
        });
        return res;
    }
    public void dfs(int[] arr, int index, ArrayList<Integer> temp){
        //if(index>=arr.length) return;
        res.add(new ArrayList<Integer>(temp));
        for(int i=index;i<arr.length;i++){
            temp.add(arr[i]);
            dfs(arr,i+1,temp);
            temp.remove(temp.size()-1);
        }
    }
}

回溯后对输出按照元素的长度排个序

全部评论

相关推荐

06-28 22:48
已编辑
广东金融学院 Java
小浪_Coding:学院本+这俩项目不是buff叠满了嘛
点赞 评论 收藏
分享
嵐jlu:我是山川🐔里🐔🧱的,阿里系简历全过; 你这简历一看就还是半成品啊,没有荣誉经历奖项什么的吗?
投递阿里巴巴集团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务