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

集合的所有子集(一)

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();
    }
}
全部评论

相关推荐

牛客848095834号:举报了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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