当前位置:网站首页>LeetCode 90:子集 II
LeetCode 90:子集 II
2022-07-07 04:27:00 【斯沃福德】
链接
题目:
思路:回溯(元素可重不可复选)
与子集不同的是题目给的元素有重复,如果按之前的做法会导致答案有重复:


这里需要添加选择时的条件以修剪节点!
①有重复元素就要先排序 !,让相同的元素靠在⼀起,
②如果发现 nums[i] == nums[i-1] 则跳过 ! 同时 i>start
Java实现:
class Solution {
List<List<Integer>> res=new LinkedList<>();
LinkedList<Integer> path=new LinkedList<>();
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
dfs(nums,0);
return res;
}
void dfs(int[] nums,int start){
res.add(new LinkedList(path));
for(int i=start;i<nums.length;i++){
//做选择
//修剪,值相同的相邻节点只遍历一次
if(i>start && nums[i]==nums[i-1]){
// i>start !
continue;
}
path.add(nums[i]);
dfs(nums,i+1);
//撤销
path.removeLast();
}
}
}
注意 :i >start !
边栏推荐
猜你喜欢

知识点滴 - 关于苹果认证MFI

为什么要了解现货黄金走势?
![[SUCTF 2019]Game](/img/9c/362117a4bf3a1435ececa288112dfc.png)
[SUCTF 2019]Game

Detailed explanation of uboot image generation process of Hisilicon chip (hi3516dv300)

Flutter riverpod is comprehensively and deeply analyzed. Why is it officially recommended?

【数学笔记】弧度
![[GUET-CTF2019]虚假的压缩包](/img/a2/7da2a789eb49fa0df256ab565d5f0e.png)
[GUET-CTF2019]虚假的压缩包

MobaXterm

图解GPT3的工作原理

Make a bat file for cleaning system garbage
随机推荐
L'externalisation a duré trois ans.
Button wizard collection learning - mineral medicine collection and running map
Jenkins remote build project timeout problem
Outsourcing for three years, abandoned
Tongda injection 0day
【obs】win-capture需要winrt
海思芯片(hi3516dv300)uboot镜像生成过程详解
nacos
微博发布案例
idea添加类注释模板和方法模板
Kbu1510-asemi power supply special 15A rectifier bridge kbu1510
Solution: could not find kf5 (missing: coreaddons dbusaddons doctools xmlgui)
Make a bat file for cleaning system garbage
通信设备商,到底有哪些岗位?
Info | webrtc M97 update
Pytorch parameter initialization
Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
Operation suggestions for today's spot Silver
Is the test cycle compressed? Teach you 9 ways to deal with it
misc ez_usb