当前位置:网站首页>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];
}
}
}
边栏推荐
- vus.SSR在asynData函数中请求数据的注意事项
- Operation suggestions for today's spot Silver
- Wechat applet data binding multiple data
- 2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
- Button wizard script learning - about tmall grabbing red envelopes
- Regular e-commerce problems part1
- Codeforces Global Round 19
- misc ez_ usb
- Technology cloud report: from robot to Cobot, human-computer integration is creating an era
- [2022 CISCN]初赛 web题目复现
猜你喜欢
随机推荐
大视频文件的缓冲播放原理以及实现
Button wizard script learning - about tmall grabbing red envelopes
What is the difference between TCP and UDP?
php导出百万数据
After the interview, the interviewer roast in the circle of friends
[2022 ciscn] replay of preliminary web topics
resource 创建包方式
C语言通信行程卡后台系统
Qt学习26 布局管理综合实例
Ansible
Iterable、Collection、List 的常见方法签名以及含义
《动手学深度学习》(四) -- 卷积神经网络 CNN
[2022 ACTF]web题目复现
JS get all date or time stamps between two time stamps
CTF daily question day43 rsa5
科技云报道:从Robot到Cobot,人机共融正在开创一个时代
【斯坦福计网CS144项目】Lab4: TCPConnection
[SUCTF 2019]Game
[GUET-CTF2019]虚假的压缩包
Resource create package method