Lintcode: k Sum II
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, [1,4] and [2,3] are possible solutions.
这道题同Combination Sum II
1 public class Solution { 2 /** 3 * @param A: an integer array. 4 * @param k: a positive integer (k <= length(A)) 5 * @param target: a integer 6 * @return a list of lists of integer 7 */ 8 public ArrayList<ArrayList<Integer>> kSumII(int A[], int k, int target) { 9 // write your code here 10 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); 11 ArrayList<Integer> path = new ArrayList<Integer>(); 12 helper(res, path, A, k, target, 0); 13 return res; 14 } 15 16 public void helper(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> path, int[] A, int k, int remain, int index) { 17 if (path.size() == k) { 18 if (remain == 0) { 19 res.add(new ArrayList<Integer>(path)); 20 } 21 return; 22 } 23 for (int i=index; i<A.length; i++) { 24 path.add(A[i]); 25 helper(res, path, A, k, remain-A[i], i+1); 26 path.remove(path.size()-1); 27 } 28 } 29 }
转载于:https://www.cnblogs.com/EdwardLiu/p/4314783.html
Lintcode: k Sum II相关推荐
- lintcode: k Sum 解题报告
K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...
- [LintCode]k Sum
题目 Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers whe ...
- LeetCode 167. Two Sum II - Input array is sorted--Python解法
题目地址:Two Sum II - Input array is sorted - LeetCode Given an array of integers that is already sorted ...
- Digit Sum II( ABC044ARC060)
问题 G: Digit Sum II 时间限制: 1 Sec 内存限制: 128 MB 提交: 36 解决: 11 [提交][状态][讨论版][命题人:admin] 题目描述 For intege ...
- [LeetCode]113.Path Sum II
[题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the giv ...
- LeetCode_Path Sum II
一.题目 Path Sum II Total Accepted: 46778 Total Submissions: 175830My Submissions Given a binary tree a ...
- 113. Path Sum II
/** 113. Path Sum II * 11.18 By Mingyang* 典型的backtracking,不过注意,这里的值可能是负数,所以不能用sum小于0来做任何判断* 1.长度标准:无 ...
- leetcode 112. Path Sum, 113. Path Sum II | 112,113. 路径总和 I, II(Java)
题目 https://leetcode.com/problems/path-sum/ https://leetcode.com/problems/path-sum-ii/ 题解 简单的遍历二叉树,不解 ...
- 2019-ACM-ICPC-南京区网络赛-E. K Sum(莫比乌斯反演 + 杜教筛)
K Sum 推式子 Fn(k)=∑l1=1n∑l2=1n⋯∑lk=1n(gcd(l1,l2,-,lk))2=∑d=1nd2∑l1=1nd∑l2=1nd⋯∑lk=1nd(gcd(l1,l2,-,lk)= ...
最新文章
- python朴素贝叶斯_Python实现的朴素贝叶斯分类器示例
- PHPCMS的产品筛选功能
- WCF中使用HttpSession
- 危难时刻,每个人都应该站出来
- 智能运维究竟能为DBA带来什么?听听4位专家怎么说
- Code POJ - 1780(栈模拟dfs)
- 【linux基础】16、软件包管理
- IOS开发-表视图LV3导航控制器
- 《Android应用开发揭秘》读者问题汇总
- C#调用API函数EnumWindows枚举窗口的方法
- 数据预处理—4.为什么要趋近于正态分布?详解
- bbsmax mysql_mysql 常用,使用经验
- 基于MATLAB的GUI界面设计流程
- 谷歌浏览器设置信任_Win10谷歌浏览器添加信任网址/站点的方法
- Effective+Java+中文版
- uvaoj 10066 - The Twin Towers 最长公共子序列(LCS)
- 蓝屏错误处理通用步骤附个人蓝屏解决步骤
- 瑞利 随机 matlab,matlab产生瑞利分布随机信号
- 我真的很郁闷,应该振作起来的
- 【PyTorch深度强化学习】带基线的蒙特卡洛策略梯度法(REINFOECE)在短走廊和CartPole环境下的实战(超详细 附源码)