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

集合的所有子集

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

来个短的

import java.util.*;
public class Solution {
    ArrayList<ArrayList<Integer>> res = new ArrayList<>();
    ArrayList<Integer> path = new ArrayList<>();

    public ArrayList<ArrayList<Integer>> subsets(int[] S) {
        Arrays.sort(S);
        dfs(S,0);
        Collections.sort(res,(o1,o2)->o1.size()-o2.size());
        return res;
    }

    public void dfs(int[] S,int index){
        res.add(new ArrayList<>(path));
        for(int i = index ; i < S.length;i++){
            path.add(S[i]);
            dfs(S,i+1);
            path.remove(path.size()-1);
        }
    }
}
全部评论

相关推荐

牛客54175811...:今年对双非很难。1、争取一段大厂实习经历,2、狂磕八股,3、再跑个难度提升的项目。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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