当前位置:网站首页>Combined sum leetcode
Combined sum leetcode
2022-06-28 14:44:00 【Hua Weiyun】
title: Combinatorial summation -Leetcode
date: 2022-04-19 09:47:00
tags: Make a little progress every day
Daily topic
subject : Combinatorial summation
To give you one No repeating elements Array of integers for candidates And a target integer target , find candidates You can make the sum of numbers as the target number target Of all Different combinations , And return... As a list . You can press In any order Return these combinations .
candidates Medium The same Numbers can Unlimited repeat selected . If the selected number of at least one number is different , Then the two combinations are different .
For a given input , Guarantee and for target The number of different combinations of is less than 150 individual .
Example :
Example 1:
Input :candidates = [2,3,6,7], target = 7
Output :[[2,2,3],[7]]
explain :
2 and 3 Can form a set of candidates ,2 + 2 + 3 = 7 . Be careful 2 You can use it multiple times .
7 Is also a candidate , 7 = 7 .
Only these two combinations .Example 2:
Input : candidates = [2,3,5], target = 8
Output : [[2,2,2,2],[2,3,3],[3,5]]Example 3:
Input : candidates = [2], target = 1
Output : []
Tips :
1 <= candidates.length <= 30
1 <= candidates[i] <= 200
candidate Every element in is Different from each other
1 <= target <= 500
Code :
class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<>(); // If the array is empty , Directly return empty results if(candidates==null||candidates.length==0){ return res; } // Prioritize Arrays.sort(candidates); //dfs dfs(candidates,target,0,new ArrayList<>(),res); return res; } public void dfs(int[] candidates,int target ,int start,List<Integer> list,List<List<Integer>> res){ // If the target value is 0, Indicates that a combination has been found , Add to result set if(target==0){ res.add(new ArrayList<>(list)); return; } // Traversal array for (int i = start; i < candidates.length; i++) { // If the current value is greater than the target value , You don't need to continue traversing if(candidates[i]>target){ return; } list.add(candidates[i]); // Update target value and starting position dfs(candidates,target-candidates[i],i,list,res); // to flash back , Delete current value list.remove(list.size()-1); } }}Daily words

The above is the sum of the combinations -Leetcode The whole content of
Copyright notice :
Original Blogger : Cowherd Conan
Personal blog links :https://www.keafmd.top/
If it helps you , Thank you for clicking on == One key, three links == Support !
[ ha-ha ][ Huai Quan ]

come on. !
Joint efforts !
Keafmd
You can see it here , You know the following , Let's make progress together !
边栏推荐
- 推荐四款可视化工具,解决 99% 的可视化大屏项目!
- 324. swinging sort II: not a simple construction problem
- What are the consequences of opening an account with Huatai Securities? How to open an account is the safest
- 叮!Techo Day 腾讯技术开放日如约而至!
- Navicat premium 16 permanent crack activation tool and installation tutorial (available for personal test)
- Flutter series: offstage in flutter
- Thread life cycle and its methods
- 名创优品通过上市聆讯:寻求双重主要上市 年营收91亿
- 智联招聘基于 Nebula Graph 的推荐实践分享
- Work study management system based on ASP
猜你喜欢

A bug liver a week I can't help mentioning issue

Ionq and Ge research confirmed that quantum computing has great potential in risk aggregation

Jingyuan's safe sprint to the Growth Enterprise Market: it plans to raise 400million yuan for investment and Yunyou software is the shareholder

Vscode writes markdown file and generates pdf

The boss told me three times: low key, low key, low key

Softing epGate PB系列网关-可将PROFIBUS总线集成到EtherNet/IP网络

美因基因港交所上市:市值43亿港元 IPO被市场忽略

Kwai investment e-commerce service provider Yixin optimization

腾讯再遭大股东Prosus减持:后者还从京东套现37亿美元

New drug discovery methods, AstraZeneca team improves ab initio molecular design through course learning
随机推荐
Ding! Techo day Tencent technology open day arrived as scheduled!
Quantum frontier hero spectrum - "light quantum Explorer" McMahon: turning any physical system into a neural network
openGauss内核:SQL解析过程分析
@Controlleradvice + @exceptionhandler handles controller layer exceptions globally
哪个证券公司最大最安全 怎么办理开户最安全
Leetcode(665)——非递减数列
2022金属非金属矿山安全检查(地下矿山)复训题库及在线模拟考试
Differences between ram ROM flash
Mingchuangyou products passed the listing hearing: seeking dual main listing with an annual revenue of 9.1 billion
干货 | 科研人的KPI怎么算,H指数和G指数是什么
猫狗图像数据集上的深度学习模型性能对比
字节跳动埋点数据流建设与治理实践
华泰证券开户有什么后果 怎么办理开户最安全
Recommended practice sharing of Zhilian recruitment based on Nebula graph
Le patron a donné trois ordres: discret, discret, discret
Unable to create process using 'd:\program file
力扣解法汇总522-最长特殊序列 II
flutter 系列之:flutter 中的 offstage
开闭原则
2022年最新PyCharm激活破解码永久_详细安装教程(适用多版本)