当前位置:网站首页>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];
}
}
}
边栏推荐
- 【斯坦福计网CS144项目】Lab4: TCPConnection
- Use and analysis of dot function in numpy
- 【webrtc】m98 screen和window采集
- [2022 actf] Web Topic recurrence
- C语言航班订票系统
- A bit of knowledge - about Apple Certified MFI
- You Li takes you to talk about C language 6 (common keywords)
- Common validation comments
- 图解GPT3的工作原理
- 探索Cassandra的去中心化分布式架构
猜你喜欢
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
Why should we understand the trend of spot gold?
Numbers that appear only once
2022年茶艺师(中级)考试试题及模拟考试
Jenkins远程构建项目超时的问题
Wechat applet data binding multiple data
通信设备商,到底有哪些岗位?
Idea add class annotation template and method template
JSON introduction and JS parsing JSON
Linux server development, SQL statements, indexes, views, stored procedures, triggers
随机推荐
Leetcode 43 String multiplication (2022.02.12)
Pytest+allure+jenkins environment -- completion of pit filling
今日现货白银操作建议
[guess-ctf2019] fake compressed packets
Zhilian + AV, AITO asked M7 to do more than ideal one
【obs】win-capture需要winrt
[Stanford Jiwang cs144 project] lab3: tcpsender
有 Docker 谁还在自己本地安装 Mysql ?
misc ez_usb
Six methods of flattening arrays with JS
misc ez_ usb
[mathematical notes] radian
[2022 ciscn] replay of preliminary web topics
[UTCTF2020]file header
Jenkins remote build project timeout problem
Technology cloud report: from robot to Cobot, human-computer integration is creating an era
CTF daily question day43 rsa5
nacos
vus. Precautions for SSR requesting data in asyndata function
【webrtc】m98 screen和window采集