最是人间留不住,朱颜辞镜花辞树。这篇文章主要讲述无标题相关的知识,希望能为你提供帮助。
题目传送地址: ??https://leetcode.cn/problems/combination-sum/submissions/??
代码如下
//递归解法
public static List<
List<
Integer>
>
combinationSum(int[] candidates, int target)
//排序
Arrays.sort(candidates);
List<
List<
Integer>
>
result = new ArrayList<
>
();
for (int i = 0;
i <
candidates.length;
i++)
List<
List<
Integer>
>
combination = combination(candidates, i, target);
if (!combination.isEmpty())
result.addAll(combination);
return result;
//算出以位置i的元素结尾,且符合最终结果的list
public static List<
List<
Integer>
>
combination(int[] candidates, int i, int target)
List<
List<
Integer>
>
result = new ArrayList<
>
();
if (i == 0)
if (target % candidates[0] == 0)
List<
Integer>
list = new ArrayList<
>
();
while (target >
0)
list.add(candidates[0]);
target = target - candidates[0];
result.add(list);
return result;
else
return result;
if (candidates[i] == target)
result.add(Arrays.asList(target));
return result;
int[] range = Arrays.copyOfRange(candidates, 0, i);
int count = 0;
while (target >
= candidates[i])
int lastIndex = i - 1;
count++;
while (lastIndex >
= 0)
List<
List<
Integer>
>
lists = combination(range, lastIndex, target - candidates[i]);
if (!lists.isEmpty())
for (List<
Integer>
list : lists)
List<
Integer>
integers = new ArrayList<
>
(list);
for (int m = 0;
m <
count;
m++)
integers.add(candidates[i]);
result.add(integers);
lastIndex--;
target = target - candidates[i];
return result;
【无标题】
推荐阅读
- ST表预处理时的数组证明
- Python编程学习(深度剖析shap.datasets.adult()源码中的X,y和X_display,y_display输出数区别)
- CertBot 更新证书失败解决
- LeetCode Algorithm 1669. 合并两个链表
- guns框架实现增删改查体会
- 2020牛客寒假算法基础集训营6.C——汉诺塔贪心 & Dilworth定理 & 二分求上升子序列最小化分数
- 2020牛客寒假算法基础集训营5.C——C语言IDE超级大模拟
- CF 1299.A——Anu Has a Function二进制
- 2020牛客寒假算法基础集训营5.J——牛牛战队的秀场