Leetcode_90 subset ll

本文提供了一种解决LeetCode 90题的方法,通过递归实现对重复元素的子集求解,确保结果中不包含重复的子集组合。代码采用Java语言实现,并对输入数组进行排序以跳过重复元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题解

这里写图片描述

题解

import java.util.*;

public class Leetcode_90 {

    public List<List<Integer>> subsetsWithDup(int[] nums) {

        List<Integer> templist = new ArrayList<>();
        List<List<Integer>> ret = new ArrayList<>();

        Arrays.sort(nums);
        findSubset(nums,templist,ret,0);
        return ret;


    }

    private void findSubset(int[] nums, List<Integer> templist, List<List<Integer>>ret, int start)
    {
        ret.add(new ArrayList<>(templist));
        for(int i = start;i<nums.length;i++)
        {
            if(i > start && nums[i] == nums[i-1]) continue;
            templist.add(nums[i]);
            findSubset(nums,templist,ret,i+1);
            templist.remove(templist.size()-1);
        }

    }


    public static void main(String[] args)
    {
        int[] nums = {1,2,2};
        Leetcode_90 a = new Leetcode_90();
        List<List<Integer>>ret = a.subsetsWithDup(nums);
        System.out.println(ret.toString());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值