累加和为 K 的子数组问题

作者:Grey

原文地址:

博客园:累加和为 K 的子数组问题

CSDN:累加和为 K 的子数组问题

题目说明

数组全为正数,且每个数各不相同,求累加和为K的子数组组合有哪些,

注:数组中同一个数字可以无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。

题目链接见:LeetCode 39. Combination Sum

主要思路

使用动态规划来解,定义如下递归函数

List<List<Integer>> p(int[] arr, int len, int i, int k)

递归含义表示:数组从 i 开始,一直到最后,可以得到的子数组满足数组之和等于 k 的子数组组合有哪些。

首先是 base case

        if (i == len) {
return new ArrayList<>();
}
List<List<Integer>> ans = new ArrayList<>();
if (k == 0) {
ans.add(new ArrayList<>());
return ans;
}

当 i 到数组结尾位置下一个位置,说明,i 到头了,不能继续往后找了,直接返回一个空列表,

当 k 等于 0,直接返回一个包含空列表的列表,表示一个数也没有,组合之和等于 0。

接下来是普遍情况,枚举每个位置有 times 个的情况下,往后收集的集合数是多少,即

for (int times = 0; times * arr[i] <= k; times++) {
// 每个位置有 times 的情况下,往后收集的集合个数
}

由于数组中全是正数,所以前提是: times * arr[i] <= k

如果times * arr[i]正好等于 k,说明收集到了一个满足条件的集合,这个集合里面有 times 个 arr[i]

for (int times = 0; times * arr[i] <= k; times++) {
// 每个位置有 times 的情况下,往后收集的集合个数
if (times * arr[i] == k) {
List<Integer> t = new ArrayList<>();
// 收集到了一种情况,即集合里面有 times 个 arr[i]
for (int j = 0; j < times; j++) {
t.add(arr[i]);
}
ans.add(t);
return ans;
}
……
}

接下来就是当前位置 i 搞定 times * arr[i],i + 1 以后的数组搞定k - times * arr[i]

        for (int times = 0; times * arr[i] <= k; times++) {
if (times * arr[i] == k) {
List<Integer> t = new ArrayList<>();
for (int j = 0; j < times; j++) {
t.add(arr[i]);
}
ans.add(t);
return ans;
}
// 剩下的位置搞定 k - arr[i] * times
for (List<Integer> a : p(arr, len, i + 1, k - times * arr[i])) {
for (int j = 0; j < times; j++) {
a.add(arr[i]);
}
ans.add(a);
}
}
return ans;

完整代码如下

class Solution {

    // 从i号位置开始及其后面的所有,帮我搞定target
public static List<List<Integer>> combinationSum(int[] arr, int k) {
return p(arr, arr.length, 0, k);
} // 从i号位置开始及其后面的所有,帮我搞定target
private static List<List<Integer>> p(int[] arr, int len, int i, int k) { if (i == len) {
return new ArrayList<>();
}
List<List<Integer>> ans = new ArrayList<>();
if (k == 0) {
ans.add(new ArrayList<>());
return ans;
} for (int times = 0; times * arr[i] <= k; times++) {
if (times * arr[i] == k) {
List<Integer> t = new ArrayList<>();
for (int j = 0; j < times; j++) {
t.add(arr[i]);
}
ans.add(t);
return ans;
}
for (List<Integer> a : p(arr, len, i + 1, k - times * arr[i])) {
for (int j = 0; j < times; j++) {
a.add(arr[i]);
}
ans.add(a);
}
}
return ans;
}
}

更多

算法和数据结构笔记

累加和为 K 的子数组问题的更多相关文章

  1. Java实现 LeetCode 713 乘积小于K的子数组(子集数量+双指针)

    713. 乘积小于K的子数组 给定一个正整数数组 nums. 找出该数组内乘积小于 k 的连续的子数组的个数. 示例 1: 输入: nums = [10,5,2,6], k = 100 输出: 8 解 ...

  2. Java实现 LeetCode 560 和为K的子数组(某著名排序大法改编)

    560. 和为K的子数组 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] ...

  3. [LeetCode]560. 和为K的子数组(前缀和)

    题目 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] 为 ...

  4. LeetCode 560. Subarray Sum Equals K (子数组之和等于K)

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  5. [Swift]LeetCode560. 和为K的子数组 | Subarray Sum Equals K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  6. [Swift]LeetCode713. 乘积小于K的子数组 | Subarray Product Less Than K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  7. 66.Subarray Sum Equals K(子数组和为K的个数)

    Level:   Medium 题目描述: Given an array of integers and an integer k, you need to find the total number ...

  8. 560. Subarray Sum Equals K 求和为k的子数组个数

    [抄题]: Given an array of integers and an integer k, you need to find the total number of continuous s ...

  9. [LeetCode] Subarray Sum Equals K 子数组和为K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

随机推荐

  1. 一文搞懂│XSS攻击、SQL注入、CSRF攻击、DDOS攻击、DNS劫持

    目录 XSS 攻击 SQL 注入 CSRF 攻击 DDOS 攻击 DNS 劫持 XSS 攻击 全称跨站脚本攻击 Cross Site Scripting 为了与重叠样式表 CSS 进行区分,所以换了另 ...

  2. 论文解读(soft-mask GNN)《Soft-mask: Adaptive Substructure Extractions for Graph Neural Networks》

    论文信息 论文标题:Soft-mask: Adaptive Substructure Extractions for Graph Neural Networks论文作者:Mingqi Yang, Ya ...

  3. LuoguP5024 保卫王国(动态DP,LCT)

    最小权覆盖集 = 全集 - 最大权独立集 强制取点.不取点可以使用把权值改成正无穷或负无穷实现 接下来就是经典的"动态最大权独立集"了 O(nlogn). 这不是我说的,是immo ...

  4. Digester解析xml原理

    Tomcat内部是使用Digester来解析xml文件的,将xml转化为java对象. digester底层是基于SAX+事件驱动+栈的方式来搭建实现的,SAX主要用来解析xml,事件驱动主要是在解析 ...

  5. 客户流失?来看看大厂如何基于spark+机器学习构建千万数据规模上的用户留存模型 ⛵

    作者:韩信子@ShowMeAI 大数据技术 ◉ 技能提升系列:https://www.showmeai.tech/tutorials/84 行业名企应用系列:https://www.showmeai. ...

  6. Vue3 Transition 过渡效果之基于 CSS 过渡

    介绍 Transistion 路由组件的切换.动态组件的切换.v-if 条件渲染组件以及 v-show 显示组件原本是没有任何过渡(CSS 动画)效果的.然而,Vue 的内置组件<Transit ...

  7. 【java】学习路线11-四种权限修饰的测试

    package com.remoo.test;public class Learn09_Test{    private static String welcomeWord1 = "你好,p ...

  8. [SDR] GNU Radio 系列教程(一) —— 什么是 GNU Radio

    目录 1.GNU Radio 是什么 2.我为什么要用 GNU Radio 3.数字信号处理 3.1 一点信号理论 3.2 将数字信号处理应用于无线电传输 4.基于流程图的模块化数字信号处理方法 本文 ...

  9. KingbaseES DENSE_RANK 函数用法

    DENSE_RANK()函数用于为结果集分区内的每一行分配一个排名,排名值之间没有差距,函数为结果集的每个分区中的每一行分配一个等级. 与 RANK() 函数不同的是,DENSE_RANK() 函数总 ...

  10. KingbaseESV8R6如何针对表单独设置vacuum策略

    背景 书接上文 KingbaseES应对表年龄增长过快导致事务回卷 ,有些特殊业务场景不能靠全局的autovacuum方法,例如大型数据库系统,频繁做update的系统,还有上文提到的做实时数据同步的 ...