当前位置:网站首页>组合总和-Leetcode
组合总和-Leetcode
2022-06-28 14:27:00 【华为云】
title: 组合总和-Leetcode
date: 2022-04-19 09:47:00
tags: 每天进步一点点系列
每日题目
题目:组合总和
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
示例:
示例 1:
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]示例 3:
输入: candidates = [2], target = 1
输出: []
提示:
1 <= candidates.length <= 30
1 <= candidates[i] <= 200
candidate 中的每个元素都 互不相同
1 <= target <= 500
代码:
class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<>(); //如果数组为空,直接返回空结果 if(candidates==null||candidates.length==0){ return res; } //先排序 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){ //如果目标值为0,说明已经找到一个组合,添加到结果集中 if(target==0){ res.add(new ArrayList<>(list)); return; } //遍历数组 for (int i = start; i < candidates.length; i++) { //如果当前值大于目标值,则不需要继续遍历 if(candidates[i]>target){ return; } list.add(candidates[i]); //更新目标值和起始位置 dfs(candidates,target-candidates[i],i,list,res); //回溯,删除当前值 list.remove(list.size()-1); } }}每日单词

以上就是组合总和-Leetcode的全部内容
版权声明:
原创博主:牛哄哄的柯南
个人博客链接:https://www.keafmd.top/
看完如果对你有帮助,感谢点击下面的==一键三连==支持!
[哈哈][抱拳]

加油!
共同努力!
Keafmd
都看到这里了,下面的内容你懂得,让我们共同进步!
边栏推荐
- Which securities company is the largest and safest? How to open an account is the safest
- 中国内地仅四家突围 联想智慧颐和园荣获 “2022年IDC亚太区智慧城市大奖”
- 【中移芯昇】5. spi接口测试tf卡
- Adding virtual environments to the Jupiter notebook
- Validate palindrome string
- Navicat Premium 16 永久破解激活工具及安装教程(亲测可用)
- 安杰思医学冲刺科创板:年营收3亿 拟募资7.7亿
- 物联网低代码平台常用《组件介绍》
- 10 key points to effectively improve performance interview
- Differences between ram ROM flash
猜你喜欢

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

Four methods of thread termination

sort

Youju new material rushes to Shenzhen Stock Exchange: it plans to raise 650million yuan, with an annual revenue of 333million yuan

线程终止的 4 种方式

字节跳动埋点数据流建设与治理实践

坐拥755万开发者的中国开源,进度几何?

【中移芯昇】5. spi接口测试tf卡

Opengauss kernel: analysis of SQL parsing process

基于 Nebula Graph 构建百亿关系知识图谱实践
随机推荐
3. Overall UI architecture of the project
Black apple installation tutorial OC boot "suggestions collection"
Can your code talk? (upper)
2022中式烹调师(高级)试题及在线模拟考试
坐拥755万开发者的中国开源,进度几何?
开源社邀您参加OpenInfra Days China 2022,议题征集进行中~
Opening and closing principle
机器人的运动范围(DFS)
验证回文串
运行近20年,基于Win 98的火星探测器软件迎来首次升级
Only four breakthrough Lenovo smart Summer Palace in mainland China won the "IDC Asia Pacific Smart City Award in 2022"
Connected to rainwater series problems
How to count dimensions of foreign trade E-mail Promotion
量子前沿英雄谱|“光量子探险家”McMahon:将任何物理系统变成神经网络
Summary of 2021 computer level III database
Which securities company is the largest and safest? How to open an account is the safest
基于 Nebula Graph 构建百亿关系知识图谱实践
2022 recurrent training question bank and online simulation examination for safety inspection of metal and nonmetal mines (underground mines)
Recommended practice sharing of Zhilian recruitment based on Nebula graph
Numbers that only appear once