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

78. Subsets(回溯)

时间:2018-04-20 17:56:11      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:output   tab   XA   list   回溯   private   bsp   clear   div   

 

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

 1 class Solution {
 2     
 3     List<List<Integer>> res = new ArrayList<>();
 4     
 5     public List<List<Integer>> subsets(int[] nums) {
 6         Arrays.sort(nums);
 7         help(new ArrayList<>(), nums, 0);
 8         return res;
 9     }
10     private void help(List<Integer> temp,int[] nums,int index){
11         temp = new ArrayList<>(temp);
12         res.add(temp);
13         for(int  i= index;i<nums.length;i++){
14             temp.add(nums[i]);
15             help(temp,nums,i+1);
16             temp.remove(temp.size()-1);
17         }
18     }
19 }

 

78. Subsets(回溯)

标签:output   tab   XA   list   回溯   private   bsp   clear   div   

原文地址:https://www.cnblogs.com/zle1992/p/8892109.html

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