首页 > 其他 > 详细

LeetCode-Subsets

时间:2016-10-01 06:11:47      阅读:270      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!