当前位置:网站首页>LeetCode 40:组合总和 II
LeetCode 40:组合总和 II
2022-07-07 04:27:00 【斯沃福德】
思路:回溯(元素可重不可复选)

组合问题和子集问题等价!问组合也就是问和为tartget的子集;
使用额外的sum来记录元素的和!sum记录的元素和path记录的元素相同,在递归到头后也和path一样撤回!
元素有重复则① 先排序 ②用 i>start && nums[i]==nums[i-1] 来筛选!
注意:
当sum已经超过target 就不需要遍历了, 节省开销!
class Solution {
List<List<Integer>> res=new LinkedList<>();
LinkedList<Integer> path=new LinkedList<>();
int sum=0;
public List<List<Integer>> combinationSum2(int[] nums, int target) {
//有重复要先排序 !
Arrays.sort(nums);
dfs(nums,target,0);
return res;
}
void dfs(int[] nums,int target,int start){
//终止条件1 :满足条件添加到res
if(sum==target){
res.add(new LinkedList(path));
return;
}
//终止条件2 :当sum大于target直接return,不然超出时间限制!
if(sum>target){
return;
}
for(int i=start;i<nums.length;i++){
//选择
if(i>start && nums[i]==nums[i-1]){
continue;
}
path.add(nums[i]);
sum+=nums[i];
//
dfs(nums,target,i+1);
//撤销
path.removeLast();
sum-=nums[i];
}
}
}
边栏推荐
- 智联+影音,AITO问界M7想干翻的不止理想One
- 4、 High performance go language release optimization and landing practice youth training camp notes
- Rxjs - observable doesn't complete when an error occurs - rxjs - observable doesn't complete when an error occurs
- 【webrtc】m98 screen和window采集
- 即刻报名|飞桨黑客马拉松第三期等你挑战
- [UTCTF2020]file header
- Route jump in wechat applet
- 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
- Solve could not find or load the QT platform plugin "xcb" in "
- Wx is used in wechat applet Showtoast() for interface interaction
猜你喜欢

After the interview, the interviewer roast in the circle of friends

Write CPU yourself -- Chapter 9 -- learning notes

SQL优化的魅力!从 30248s 到 0.001s

Resource create package method

Bi she - college student part-time platform system based on SSM

How can a 35 year old programmer build a technological moat?
![[experience sharing] how to expand the cloud service icon for Visio](/img/42/dba9f78f3fb2049dad8b343b0b36e5.png)
[experience sharing] how to expand the cloud service icon for Visio
![[SUCTF 2019]Game](/img/9c/362117a4bf3a1435ececa288112dfc.png)
[SUCTF 2019]Game
![[2022 CISCN]初赛 web题目复现](/img/1c/4297379fccde28f76ebe04d085c5a4.png)
[2022 CISCN]初赛 web题目复现
![[UTCTF2020]file header](/img/e3/818e2d531a06ab90de189055f634ad.png)
[UTCTF2020]file header
随机推荐
【性能压测】如何做好性能压测?
Pytest + allure + Jenkins Environment - - achèvement du remplissage de la fosse
leetcode:105. 从前序与中序遍历序列构造二叉树
[unity] several ideas about circular motion of objects
English translation is too difficult? I wrote two translation scripts with crawler in a rage
[cloud native] how to give full play to memory advantage of memory database
微信小程序中使用wx.showToast()进行界面交互
智联+影音,AITO问界M7想干翻的不止理想One
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
IO流 file
Build personal website based on flask
1141_ SiCp learning notes_ Functions abstracted as black boxes
Asemi rectifier bridge rs210 parameters, rs210 specifications, rs210 package
UWB learning 1
php导出百万数据
Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
【斯坦福计网CS144项目】Lab3: TCPSender
按键精灵脚本学习-关于天猫抢红包
科技云报道:从Robot到Cobot,人机共融正在开创一个时代
微博发布案例
