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

78. Subsets

时间:2016-10-06 15:03:59      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

不定期更新leetcode解题java答案。

采用pick one的方式选择题目。

题意为给定一个数组,返回所有子集的集合。

采用递归的方式,逐步由空集合增加至最大数量集合。代码如下:

 1 public class Solution {
 2     public List<List<Integer>> subsets(int[] nums) {
 3         List<List<Integer>> list = new ArrayList();
 4         
 5         List<Integer> nul = new ArrayList();
 6         list.add(nul);
 7         
 8         addSubsets(list, nul, 0, nums);
 9         
10         return list;
11     }
12     
13     public void addSubsets(List<List<Integer>> list, List<Integer> init, int location, int[] nums){
14         for(int i = location; i < nums.length; i++){
15             List<Integer> tmpList = new ArrayList();
16             
17             for(int j = 0; j < init.size(); j++)
18                 tmpList.add(init.get(j));
19                 
20             tmpList.add(nums[i]);
21             list.add(tmpList);
22             
23             addSubsets(list, tmpList, i + 1, nums);
24         }
25     }
26 }

 

78. Subsets

标签:

原文地址:http://www.cnblogs.com/zslhq/p/5933790.html

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