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

Leetcode 78. Subsets (backtracking) 90 subset

时间:2018-05-15 14:01:29      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:backtrac   return   rem   etc   leetcode   color   rev   subsets   length   

using prev

class Solution {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    public List<List<Integer>> subsets(int[] nums) {
        List<Integer> temp = new ArrayList<>();
        back(temp,nums, 0);
        return res;
    }
    void back(List<Integer> temp,int[] nums, int prev){
        List<Integer> list = new ArrayList<>(temp);
        //if(!res.contains(list))
        res.add(list);
        
        for(int i = prev; i<nums.length; i++){
            temp.add(nums[i]);
            back(temp, nums, i+1);
            temp.remove(temp.size()-1);
        }
    }
}

 

// 90 subset2

sorting the array first and check the contains

class Solution {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<Integer> temp = new ArrayList<>();
        Arrays.sort(nums);
        back(temp,nums, 0);
        return res;
    }
    void back(List<Integer> temp,int[] nums, int prev){
        List<Integer> list = new ArrayList<>(temp);
        if(!res.contains(list))
        res.add(list);
        
        for(int i = prev; i<nums.length; i++){
            temp.add(nums[i]);
            back(temp, nums, i+1);
            temp.remove(temp.size()-1);
        }
    }

}

 

Leetcode 78. Subsets (backtracking) 90 subset

标签:backtrac   return   rem   etc   leetcode   color   rev   subsets   length   

原文地址:https://www.cnblogs.com/stiles/p/Leetcode78.html

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