首页 > 其他 > 详细

Leetcode 78. Subsets (backtracking) 90 subset

时间:2018-05-15 13:54:52      阅读:210      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!