当前位置:网站首页>Leetcode 40: combined sum II
Leetcode 40: combined sum II
2022-07-07 07:53:00 【Swarford】
Ideas : to flash back ( Elements can be duplicated and cannot be checked )

The combination problem is equivalent to the subset problem ! Question combination means asking and doing tartget Subset ;
Use extra sum To record the sum of elements !sum Recorded elements and path The recorded elements are the same , After recursion ends, it is also connected with path Same withdrawal !
Elements Repeat be ① First Sort ② use i>start && nums[i]==nums[i-1] To screen !
Be careful :
When sum Has more than target There's no need to traverse , Save money !
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) {
// If there is repetition, you should sort first !
Arrays.sort(nums);
dfs(nums,target,0);
return res;
}
void dfs(int[] nums,int target,int start){
// Termination conditions 1 : Meet the conditions to add to res
if(sum==target){
res.add(new LinkedList(path));
return;
}
// Termination conditions 2 : When sum Greater than target direct return, Otherwise, the time limit is exceeded !
if(sum>target){
return;
}
for(int i=start;i<nums.length;i++){
// choice
if(i>start && nums[i]==nums[i-1]){
continue;
}
path.add(nums[i]);
sum+=nums[i];
//
dfs(nums,target,i+1);
// revoke
path.removeLast();
sum-=nums[i];
}
}
}
边栏推荐
- Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
- [CV] Wu Enda machine learning course notes | Chapter 8
- 【Unity】物体做圆周运动的几个思路
- Cnopendata American Golden Globe Award winning data
- What is the interval in gatk4??
- Linux server development, MySQL stored procedures, functions and triggers
- Wechat applet data binding multiple data
- After the interview, the interviewer roast in the circle of friends
- pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
- 2022焊工(初级)判断题及在线模拟考试
猜你喜欢

【斯坦福计网CS144项目】Lab3: TCPSender

即刻报名|飞桨黑客马拉松第三期等你挑战

JSON introduction and JS parsing JSON

@component(““)

Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)

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

解决问题:Unable to connect to Redis

mysql多列索引(组合索引)特点和使用场景

开源生态|打造活力开源社区,共建开源新生态!

Is the test cycle compressed? Teach you 9 ways to deal with it
随机推荐
Qt学习27 应用程序中的主窗口
Solution: could not find kf5 (missing: coreaddons dbusaddons doctools xmlgui)
自定义类加载器加载网络Class
JSON introduction and JS parsing JSON
知识点滴 - 关于苹果认证MFI
Installing postgresql11 database under centos7
What is the interval in gatk4??
【斯坦福计网CS144项目】Lab4: TCPConnection
Linux server development, MySQL stored procedures, functions and triggers
Visualization Document Feb 12 16:42
ASEMI整流桥RS210参数,RS210规格,RS210封装
[unity] several ideas about circular motion of objects
Linux server development, MySQL process control statement
【经验分享】如何为visio扩展云服务图标
What are the positions of communication equipment manufacturers?
【Unity】物体做圆周运动的几个思路
@component(““)
vus.SSR在asynData函数中请求数据的注意事项
[UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
Use and analysis of dot function in numpy
