当前位置:网站首页>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 !
边栏推荐
- 基于asp.net的文献检索系统
- VPS是干嘛用的?有哪些知名牌子?与云服务器有什么区别?
- 2022金属非金属矿山安全检查(地下矿山)复训题库及在线模拟考试
- 股票开户优惠链接,我如何才能得到?手机开户是安全么?
- How to handle the safest account opening with Huatai Securities app
- Introduction to common components of IOT low code platform
- Four visualization tools are recommended to solve 99% of large screen visualization projects!
- 2022中式烹調師(高級)試題及在線模擬考試
- RAM ROM FLASH的区别
- CVPR disputes again: IBM's Chinese draft papers were accused of copying idea, who won the second place in the competition
猜你喜欢

Thread life cycle and its methods

运行近20年,基于Win 98的火星探测器软件迎来首次升级

荐书丨《大脑通信员》:如果爱情只是化学反应,那还能相信爱情吗?

2022下半年软考考试时间安排已确定!

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

Ding! Techo day Tencent technology open day arrived as scheduled!

New drug discovery methods, AstraZeneca team improves ab initio molecular design through course learning

ArcGIS vector center point generates rectangle and cuts TIF image for deep learning sample training

Vscode writes markdown file and generates pdf

Four visualization tools are recommended to solve 99% of large screen visualization projects!
随机推荐
open3d里pointcloud和numpy数组之间的转化
Le patron a donné trois ordres: discret, discret, discret
Youju new material rushes to Shenzhen Stock Exchange: it plans to raise 650million yuan, with an annual revenue of 333million yuan
The time schedule for the soft test in the second half of 2022 has been determined!
IonQ联合GE Research证实:量子计算在风险聚合上有巨大潜力
Seata数据库中出现以下问题要怎么解决啊?
PMP认证证书的续证费用是多少?
【数字IC精品文章收录】近500篇文章|学习路线|基础知识|接口|总线|脚本语言|芯片求职|安全|EDA|工具|低功耗设计|Verilog|低功耗|STA|设计|验证|FPGA|架构|AMBA|书籍|
Recommended practice sharing of Zhilian recruitment based on Nebula graph
openGauss内核:SQL解析过程分析
How to count dimensions of foreign trade E-mail Promotion
运行近20年,基于Win 98的火星探测器软件迎来首次升级
Configuration file encryption (simple use of jasypt)
外贸邮件推广怎么统计维度
Jingyuan's safe sprint to the Growth Enterprise Market: it plans to raise 400million yuan for investment and Yunyou software is the shareholder
Leetcode (88) -- merge two ordered arrays
Conversion between pointcloud and numpy arrays in open3d
Vscode writes markdown file and generates pdf
外贸SEO 站长工具
Robot range of motion (DFS)