Leetcode no. 78

来源:互联网 发布:看美股行情软件 编辑:程序博客网 时间:2024/06/06 09:45

78. Subsets

 

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

Note:

  • Elements in a subset must be in non-descending order.
  • 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],  []]
public class Solution {    public List<List<Integer>> subsets(int[] nums) {        List<List<Integer>> list= new LinkedList<>();        if (nums.length==0) return list;        Arrays.sort(nums);        List<Integer> lzero= new LinkedList<>();        list.add(lzero);        for (int i = 0; i < nums.length; i++) {            List<List<Integer>> tmplist= new LinkedList<>();            for (List<Integer> ele: list) {                List<Integer> it= new LinkedList<>(ele);                it.add(nums[i]);                tmplist.add(it);            }            list.addAll(tmplist);        }        return list;    }}


 

0 0
原创粉丝点击