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相关推荐

  1. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

  2. [LintCode]k Sum

    题目 Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers whe ...

  3. 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 ...

  4. Digit Sum II( ABC044ARC060)

    问题 G: Digit Sum II 时间限制: 1 Sec  内存限制: 128 MB 提交: 36  解决: 11 [提交][状态][讨论版][命题人:admin] 题目描述 For intege ...

  5. [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 ...

  6. LeetCode_Path Sum II

    一.题目 Path Sum II Total Accepted: 46778 Total Submissions: 175830My Submissions Given a binary tree a ...

  7. 113. Path Sum II

    /** 113. Path Sum II * 11.18 By Mingyang* 典型的backtracking,不过注意,这里的值可能是负数,所以不能用sum小于0来做任何判断* 1.长度标准:无 ...

  8. 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/ 题解 简单的遍历二叉树,不解 ...

  9. 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)= ...

最新文章

  1. python朴素贝叶斯_Python实现的朴素贝叶斯分类器示例
  2. PHPCMS的产品筛选功能
  3. WCF中使用HttpSession
  4. 危难时刻,每个人都应该站出来
  5. 智能运维究竟能为DBA带来什么?听听4位专家怎么说
  6. Code POJ - 1780(栈模拟dfs)
  7. 【linux基础】16、软件包管理
  8. IOS开发-表视图LV3导航控制器
  9. 《Android应用开发揭秘》读者问题汇总
  10. C#调用API函数EnumWindows枚举窗口的方法
  11. 数据预处理—4.为什么要趋近于正态分布?详解
  12. bbsmax mysql_mysql 常用,使用经验
  13. 基于MATLAB的GUI界面设计流程
  14. 谷歌浏览器设置信任_Win10谷歌浏览器添加信任网址/站点的方法
  15. Effective+Java+中文版
  16. uvaoj 10066 - The Twin Towers 最长公共子序列(LCS)
  17. 蓝屏错误处理通用步骤附个人蓝屏解决步骤
  18. 瑞利 随机 matlab,matlab产生瑞利分布随机信号
  19. 我真的很郁闷,应该振作起来的
  20. 【PyTorch深度强化学习】带基线的蒙特卡洛策略梯度法(REINFOECE)在短走廊和CartPole环境下的实战(超详细 附源码)

热门文章

  1. Linux上DNS实现工具之bind详叙
  2. [分享]千万数量级分页存储过程(效果演示)
  3. control theory and application
  4. 吸收和实践的同时推进
  5. 关于armv7指令集的一个直观数据
  6. 使用QT Creator开发C++程序
  7. SQL Challenge ——快速找到1-100之间缺失的数
  8. 项目托管到GitHub及简单使用
  9. 垂直居中 absolute 和 flex 方法
  10. Java新手入门的30个基本概念