Leetcode Subsets

来源:互联网 发布:mac safari 缓存路径 编辑:程序博客网 时间:2024/06/06 10:59

Given a set of distinct integers, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,3], a solution is:

[  [3],  [1],  [2],  [1,2,3],  [1,3],  [2,3],  [1,2],  []]

Difficulty: Medium


public class Solution {    public List<List<Integer>> subsets(int[] nums) {        List<List<Integer>> res = new ArrayList<List<Integer>>();        res.add(new ArrayList<Integer>());        for(int i = 0; i < nums.length; i++){            int len = res.size();            for(int j = 0; j < len; j++){                List<Integer> temp = new ArrayList<Integer>(res.get(j));                temp.add(nums[i]);                res.add(temp);            }        }        return res;    }}


0 0
原创粉丝点击