当前位置:网站首页>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);
}
}
}
边栏推荐
- The first common node of the two linked lists of "Jianzhi offer"
- Two stage submission and three stage submission
- Notice of Nanshan District Civil Affairs Bureau on carrying out the grade evaluation of social organizations in Nanshan District in 2022
- STM32 can -- can ID filter analysis
- Introduction to STM32 learning can controller
- 3D相关的简单数学知识
- JMeter recording interface automation
- 基于FIFO IDT7202-12的数字存储示波器
- MOS管防止电源反接的原理
- What is tor? What is the use of tor browser update?
猜你喜欢

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

基于stm32的数字示波器设计方案

Watermelon book machine learning reading notes Chapter 1 Introduction

LeetCode 190. 颠倒二进制位 位运算/easy

Leetcode-1737- minimum number of characters to change if one of the three conditions is met

反射

Leetcode-1737-满足三条件之一需改变的最少字符数

LeetCode 240. 搜索二维矩阵 II medium

Several basic uses of tl431-2.5v voltage reference chip
仪表放大器和运算放大器优缺点对比
随机推荐
泛型
Code coverage statistical artifact -jacobo tool practice
基于stm32的数字示波器设计方案
网络设备硬核技术内幕 路由器篇 3 贾宝玉梦游太虚幻境 (中)
Basic usage of kotlin
Kotlin的基础用法
Huayun data creates a perfect information technology and innovation talent training system to help the high-quality development of information technology and innovation industry
Unity3D学习笔记10——纹理数组
两阶段提交与三阶段提交
仪表放大器和运算放大器优缺点对比
Design scheme of digital oscilloscope based on stm32
网络设备硬核技术内幕 路由器篇 19 DPDK(四)
Reading notes of lifelong growth (I)
STM32 can -- can ID filter analysis
CAN总线的EMC设计方案
《剑指Offer》 合并两个排序的链表
4种单片机驱动继电器方案
Network equipment hard core technology insider router Chapter 3 Jia Baoyu sleepwalking in Taixu Fantasy (middle)
The mobile terminal uses the list component of vantui. When multiple tab items are switched back and forth, the list is loaded many times, resulting in the failure of normal display of data
Network equipment hard core technology insider router Chapter 4 Jia Baoyu sleepwalking in Taixu Fantasy (Part 2)