题解 | #集合的所有子集(一)#

集合的所有子集(一)

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

import java.util.*;

public class Solution {
    public ArrayList<ArrayList<Integer>> subsets(int[] nums) {
        ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
        process(nums, 0, ans, new LinkedList<>());
        return ans;
    }

    public static void process(int[] nums, int index, ArrayList<ArrayList<Integer>> ans,
                               LinkedList<Integer> list) {
        if (index == nums.length) {
            ans.add(new ArrayList<>(list));
            return;
        }
        process(nums, index + 1, ans, list);
        list.add(nums[index]);
        process(nums, index + 1, ans, list);
        list.removeLast();
    }
}
全部评论

相关推荐

05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-20 18:18
是不是意味着秋招就完蛋了
花不开柳成荫:如果你是Java,是的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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