*[Lintcode]k Sum II
来源:互联网 发布:淘宝文案策划待遇 编辑:程序博客网 时间:2024/06/06 13:33
Given n unique integers, number k (1<=k<=n) and target.
Find all possible k integers where their sum is target.
Example
Given [1,2,3,4]
, k = 2
, target = 5
. Return:
[ [1,4], [2,3]]
分析:递归
public class Solution { /** * @param A: an integer array. * @param k: a positive integer (k <= length(A)) * @param target: a integer * @return a list of lists of integer */ public ArrayList<ArrayList<Integer>> kSumII(int[] A, int k, int target) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); helper(A, k, target, 0, res, new ArrayList<Integer>()); return res; } void helper(int[] A, int k, int target, int index, ArrayList<ArrayList<Integer>> res, ArrayList<Integer> list) { if(target == 0 && k == 0) { res.add(new ArrayList<Integer>(list)); return; } if(index >= A.length || target < 0 || k < 0) return; for(int i = index; i < A.length; i++) { list.add(A[i]); helper(A, k - 1, target - A[i], i + 1, res, list); list.remove(list.size() - 1); } }}
0 0
- Lintcode - k sum II
- [LintCode]k Sum II
- lintcode:k Sum II
- *[Lintcode]k Sum II
- [LintCode]k Sum II
- [LintCode]k Sum
- [LintCode]k Sum
- [Lintcode] Combination Sum II
- [LintCode] Continuous Subarray Sum II
- [刷题]k Sum II
- #90 k Sum II
- LintCode-k数和 II
- LintCode k数和 II
- k数和 II-LintCode
- [Lintcode]Interval Sum II区间求和 II
- [lintcode] Binary Tree Maximum Path Sum II
- lintcode- k数和II-90
- lintcode 90. k数和 II DFS
- 动态规划——看似dp的贪心问题最大乘积(蓝桥杯试题集)
- linux进程调度 - 组织进程
- Android Material Design兼容库控件使用(一)
- 网络爬虫学习笔记——GET方法下几种常用情况
- 新增非空约束字段在不同版本中的演进
- *[Lintcode]k Sum II
- DatabaseTest
- 死循环-内存溢出
- python中的闭包与装饰器
- 第十一周项目1——二叉树算法验证(2)二叉树构造算法验证
- 分布式事务之2PC和3PC
- git同步本地仓库和远程仓库
- C#第八章上机4
- SQL 分页支持查询 ROW_NUMBER() OVER (ORDER BY ReportID) AS RowNo