当前位置:网站首页>组合总和-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
都看到这里了,下面的内容你懂得,让我们共同进步!
边栏推荐
- 哪个证券公司最大最安全 怎么办理开户最安全
- 求解汉诺塔问题
- 证券公司和银行哪个更安全 怎么办理开户最安全
- Navicat Premium 16 永久破解激活工具及安装教程(亲测可用)
- Thread life cycle and its methods
- Rslo: self supervised lidar odometer (real time + high precision, icra2022)
- Opengauss kernel: analysis of SQL parsing process
- Only four breakthrough Lenovo smart Summer Palace in mainland China won the "IDC Asia Pacific Smart City Award in 2022"
- Ding! Techo day Tencent technology open day arrived as scheduled!
- Based on asp Net based document retrieval system
猜你喜欢

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

你的代碼會說話嗎?(上)

基于MATLAB的混沌数字图像加密技术研究与仿真实现

PC博物馆-熟悉又陌生的懵懂年代

Four visualization tools are recommended to solve 99% of large screen visualization projects!

Based on asp Net based document retrieval system

Mulan open work license 1.0 open to the public for comments

Source code analysis of ArrayList

Only four breakthrough Lenovo smart Summer Palace in mainland China won the "IDC Asia Pacific Smart City Award in 2022"

Reading notes of Mr. toad going to see a psychologist
随机推荐
快手投资电商服务商易心优选
Quantum frontier hero spectrum - "light quantum Explorer" McMahon: turning any physical system into a neural network
Nature | mapping the interaction map of plant foliar flora to establish genotype phenotype relationship
解决Unable to create process using ‘D:\Program File
基于 Nebula Graph 构建百亿关系知识图谱实践
Validate palindrome string
求解汉诺塔问题
Opening and closing principle
优巨新材冲刺深交所:拟募资6.5亿 年营收3.33亿
Four visualization tools are recommended to solve 99% of large screen visualization projects!
Mingchuangyou products passed the listing hearing: seeking dual main listing with an annual revenue of 9.1 billion
Practice of constructing ten billion relationship knowledge map based on Nebula graph
等保备案主体是谁?在当地网安进行备案是吗?
Reverse a stack with recursive functions and stack operations only
Connected to rainwater series problems
字节跳动埋点数据流建设与治理实践
How to design data visualization platform
Leetcode(406)——根据身高重建队列
单一职责原则
2022 operation of simulation test platform for 100 simulated questions of main principals of metal and nonmetal mines (underground mines)