码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode-Subsets

时间:2016-10-01 06:13:22      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

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],
  []
]

 

public class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        if(nums==null){
            return null;
        }
        List<List<Integer>> resList=new ArrayList<List<Integer>>();
        List<Integer> item=new ArrayList<Integer>();
        Arrays.sort(nums);
        backTracking(nums, 0, item, resList);
        resList.add(new ArrayList<Integer>());
        return resList;
    }
    
    public void backTracking(int[] nums, int start, List<Integer> item,  List<List<Integer>> resList){
        
        for(int i=start; i<nums.length; i++){
            item.add(nums[i]);
            resList.add(new ArrayList<Integer>(item));
            backTracking(nums, i+1, item, resList);
            item.remove(item.size()-1);
        }
    }
}

 

LeetCode-Subsets

标签:

原文地址:http://www.cnblogs.com/incrediblechangshuo/p/5925380.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!