刷刷刷 Day 28 | 90. 子集 II
90. 子集 II
LeetCode题目要求
给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。
示例
输入:nums = [1,2,2] 输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]
解题思路
上代码
class Solution { List<List<Integer>> res = new ArrayList<>(); Deque<Integer> path = new LinkedList<>(); public List<List<Integer>> subsetsWithDup( int[] nums ) { // 先排序 Arrays.sort( nums ); backtracking( nums, 0 ); return res; } private void backtracking(int[] nums, int start ) { res.add( new ArrayList<>( path ) ); for ( int i = start; i < nums.length; i++ ) { // 跳过当前树层使用过的、相同的元素 if ( i > start && nums[i - 1] == nums[i] ) { continue; } path.add( nums[i] ); subsetsWithDupHelper( nums, i + 1 ); path.removeLast(); } } }
附:学习资料链接
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了