当前位置:网站首页>Leetcode 90. subset II backtracking /medium
Leetcode 90. subset II backtracking /medium
2022-07-27 15:27:00 【Abcheche】
List of articles
1.Description
Give you an array of integers nums , It may contain repeating elements , Please return all possible subsets of the array ( Power set ).
Solution set You can't Contains a subset of repetitions . The solution set returned , Subsets can be classified by In any order array .
2.Example
Input :nums = [1,2,2]
Output :[[],[1],[1,2],[1,2,2],[2],[2,2]]
3.Solution
Using backtracking algorithm , Pay attention to whether the element to be added is the same as the previous element when recursing , Skip if the same , It should be that if there are the same elements in this layer, there will be repeated subsets in the next layer .
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> path = new ArrayList<Integer>();
Arrays.sort(nums);
backtrack(res,path,nums,0);
return res;
}
public static void backtrack(List<List<Integer>> res,List<Integer> path,int[] nums,int index){
if(index > nums.length) {
return ;
}
res.add(new ArrayList<>(path));
for(int i=index;i<nums.length;i++) {
if(i!=index&&nums[i]==nums[i-1]) {
continue;
}
path.add(nums[i]);
backtrack(res, path, nums, i+1);
path.remove(path.size()-1);
}
}
}
边栏推荐
- 网络设备硬核技术内幕 路由器篇 10 CISCO ASR9900拆解 (三)
- DIY制作示波器的超详细教程:(一)我不是为了做一个示波器
- 网络设备硬核技术内幕 路由器篇 (10) CISCO ASR9900拆解 (四)
- Unity mouse controls the first person camera perspective
- EMC design scheme of RS485 interface
- 3.3-5v conversion
- 《剑指Offer》数组中的逆序对
- Leetcode-1737- minimum number of characters to change if one of the three conditions is met
- "Sword finger offer" linked list inversion
- 初探STM32掉电复位PDR
猜你喜欢

LeetCode 面试题 17.21. 直方图的水量 双指针,单调栈/hard

LeetCode 781. 森林中的兔子 哈希表/数学问题 medium

Dialog manager Chapter 3: create controls

LeetCode 783. 二叉搜索树节点最小距离 树/easy

光电隔离电路设计方案(六款基于光耦、AD210AN的光电隔离电路图)

Unity mouse controls the first person camera perspective

华云数据打造完善的信创人才培养体系 助力信创产业高质量发展

ad7606与stm32连接电路介绍

CAN总线的EMC设计方案

Several basic uses of tl431-2.5v voltage reference chip
随机推荐
Network equipment hard core technology insider router Chapter 10 Cisco asr9900 disassembly (III)
LeetCode 190. 颠倒二进制位 位运算/easy
After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues
LeetCode 面试题 17.21. 直方图的水量 双指针,单调栈/hard
MySQL interview 40 consecutive questions, interviewer, if you continue to ask, I will turn my face
华云数据打造完善的信创人才培养体系 助力信创产业高质量发展
USB2.0接口的EMC设计方案
多线程环境下CountDownLatch的用法
USB interface electromagnetic compatibility (EMC) solution
Dialog manager Chapter 3: create controls
Problem solving in magic tower project
LeetCode 240. 搜索二维矩阵 II medium
事务_基本演示和事务_默认自动提交&手动提交
Unity performance optimization ----- drawcall
JUC(JMM、Volatile)
仪表放大器和运算放大器优缺点对比
工具 - markdown编辑器常用方法
LeetCode 783. 二叉搜索树节点最小距离 树/easy
网络设备硬核技术内幕 路由器篇 21 可重构的路由器
网络设备硬核技术内幕 路由器篇 20 DPDK (五)