0
点赞
收藏
分享

微信扫一扫

力扣算法学习day20-3

静悠 2022-02-09 阅读 44

文章目录

力扣算法学习day20-3

78-子集

题目

image-20220209210615870

代码实现

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
        recall(0,nums);
        
        return result;
    }
    public void recall(int i,int[] nums){
        result.add(new ArrayList<>(path));
        
        for(int j = i;j < nums.length;j++){
            path.add(nums[j]);
            recall(j+1,nums);
            path.remove(path.size()-1);
        }
    }
}

90-子集 II

题目

image-20220209213742952

代码实现

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        recall(0,nums);

        return result;
    }
    public void recall(int i,int[] nums){
        result.add(new ArrayList<>(path));

        for(int j = i;j < nums.length;j++){
            if(j > i && nums[j] == nums[j - 1]){
                continue;
            }
            
            path.add(nums[j]);
            recall(j+1,nums);
            path.remove(path.size()-1);
        }
    }
}

491-递增子序列,1.5h尝试失败,明日修改

已复习 242-有效的字母异位词

举报

相关推荐

0 条评论