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

Subsets; BackTracking; DFS; Bit Manipulation; DP;

时间:2016-01-22 13:43:25      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

Based on the combinations problem, we use a for loop to call the method created in that problem and this problem will be solved. Later we‘ll add bit manipulation.

Code:

public class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> resultList = new ArrayList<>();
        int len = nums.length;
        
        for(int i = 0; i < len; i++){
            List<Integer> list = new ArrayList<>();
            addSubset(nums, len-1, 0, i+1, resultList, list);
        }
        List<Integer> list = new ArrayList<>();
        resultList.add(list);
        return resultList;
    }
    
    public void addSubset(int[] nums, int n, int cur, int k, List<List<Integer>> result, List<Integer> list){
        if(k == 0){
            result.add(new ArrayList<>(list));
            return;
        }
        
        list.add(nums[cur]);
        addSubset(nums, n, cur+1, k-1, result, list);
        list.remove(list.size()-1);
        if(cur == n-k+1) return;
        addSubset(nums, n, cur+1, k, result, list);
    }
}

 

Subsets; BackTracking; DFS; Bit Manipulation; DP;

标签:

原文地址:http://www.cnblogs.com/5683yue/p/5150767.html

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