当前位置:网站首页>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 !
边栏推荐
- [ANSYS] learning experience of APDL finite element analysis
- [performance pressure test] how to do a good job of performance pressure test?
- Common method signatures and meanings of Iterable, collection and list
- vus. Precautions for SSR requesting data in asyndata function
- Tencent's one-day life
- Leetcode-543. Diameter of Binary Tree
- JS get all date or time stamps between two time stamps
- [GUET-CTF2019]虚假的压缩包
- [UTCTF2020]file header
- gslx680触摸屏驱动源码码分析(gslX680.c)
猜你喜欢
Simple example of ros2 planning system plansys2
misc ez_ usb
leetcode:105. Constructing binary trees from preorder and inorder traversal sequences
Bi she - college student part-time platform system based on SSM
Flutter riverpod is comprehensively and deeply analyzed. Why is it officially recommended?
1、 Go knowledge check and remedy + practical course notes youth training camp notes
1141_ SiCp learning notes_ Functions abstracted as black boxes
KBU1510-ASEMI电源专用15A整流桥KBU1510
About some details of final, I have something to say - learn about final CSDN creation clock out from the memory model
[2022 actf] Web Topic recurrence
随机推荐
解决:Could NOT find KF5 (missing: CoreAddons DBusAddons DocTools XmlGui)
Pytest+allure+jenkins environment -- completion of pit filling
知识点滴 - 关于苹果认证MFI
Tianqing sends instructions to bypass the secondary verification
毕设-基于SSM大学生兼职平台系统
Talk about seven ways to realize asynchronous programming
2022-07-06:以下go语言代码是否会panic?A:会;B:不会。 package main import “C“ func main() { var ch chan struct
buuctf misc USB
Codeforces Global Round 19
【webrtc】m98 screen和window采集
2022-07-06: will the following go language codes be panic? A: Meeting; B: No. package main import “C“ func main() { var ch chan struct
Outsourcing for three years, abandoned
misc ez_ usb
Solution: could not find kf5 (missing: coreaddons dbusaddons doctools xmlgui)
Pytorch parameter initialization
Common method signatures and meanings of Iterable, collection and list
A bit of knowledge - about Apple Certified MFI
Solve could not find or load the QT platform plugin "xcb" in "
Button wizard script learning - about tmall grabbing red envelopes
[GUET-CTF2019]虚假的压缩包