当前位置:网站首页>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];
}
}
}
边栏推荐
- Figure out the working principle of gpt3
- Is the test cycle compressed? Teach you 9 ways to deal with it
- mysql多列索引(组合索引)特点和使用场景
- Numbers that appear only once
- vus. Precautions for SSR requesting data in asyndata function
- JS get all date or time stamps between two time stamps
- Rust Versus Go(哪种是我的首选语言?)
- Problem solving: unable to connect to redis
- 【webrtc】m98 screen和window采集
- C语言航班订票系统
猜你喜欢
2022年茶艺师(中级)考试试题及模拟考试
Qt学习26 布局管理综合实例
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
科技云报道:从Robot到Cobot,人机共融正在开创一个时代
开源生态|打造活力开源社区,共建开源新生态!
Cnopendata American Golden Globe Award winning data
今日现货白银操作建议
misc ez_ usb
[mathematical notes] radian
Common method signatures and meanings of Iterable, collection and list
随机推荐
Pytest+allure+jenkins installation problem: pytest: error: unrecognized arguments: --alluredir
Common validation comments
[P2P] local packet capturing
Figure out the working principle of gpt3
Weibo publishing cases
Technology cloud report: from robot to Cobot, human-computer integration is creating an era
Tongda injection 0day
nacos
gslx680触摸屏驱动源码码分析(gslX680.c)
PHP exports millions of data
【webrtc】m98 screen和window采集
Detailed explanation of uboot image generation process of Hisilicon chip (hi3516dv300)
nacos
[UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
dash plotly
解决could not find or load the Qt platform plugin “xcb“in ““.
智联+影音,AITO问界M7想干翻的不止理想One
Leanote private cloud note building
paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
[webrtc] m98 Screen and Window Collection