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
 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) {
         // write your code here
         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
         ArrayList<Integer> path = new ArrayList<Integer>();
         helper(res, path, A, k, target, 0);
         return res;
     }
     public void helper(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> path, int[] A, int k, int remain, int index) {
         if (path.size() == k) {
             if (remain == 0) {
                 res.add(new ArrayList<Integer>(path));
             }
             return;
         }
         for (int i=index; i<A.length; i++) {
             path.add(A[i]);
             helper(res, path, A, k, remain-A[i], i+1);
             path.remove(path.size()-1);
         }
     }
 }
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 ii k数和 II
		
题目: k数和 II 给定n个不同的正整数,整数k(1<= k <= n)以及一个目标数字. 在这n个数里面找出K个数,使得这K个数的和等于目标数字,你需要找出所有满足要求的方案. 样例 ...
 - Lintcode: Interval Sum II
		
Given an integer array in the construct method, implement two methods query(start, end) and modify(i ...
 - LintCode "k Sum" !!
		
Great great DP learning experience:http://www.cnblogs.com/yuzhangcmu/p/4279676.html Remember 2 steps ...
 - LintCode: Combination Sum II
		
C++ DFS class Solution { public: void help(vector<int> &a, int now, int sum, int target, v ...
 - LintCode "Subarray Sum II"
		
Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you ...
 - k Sum | & ||
		
k Sum Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers ...
 - [LintCode] Subarray Sum & Subarray Sum II
		
Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...
 - Continuous Subarray Sum II(LintCode)
		
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
 
随机推荐
- windows 电脑配置信息检测
			
内存条 DDR4 DDR4相比DDR3最大的区别有: 1)处理器:每次内存升级换代时,必须支持的就是处理器.Haswell-E平台的内存同IVB-E/SNB-E一样为四通道设计,DDR4内存频率原生支 ...
 - 使用virtualbox 配置 linux host-only虚拟主机连接外网(转载)
			
host-only 下的虚拟机之间可以互相访问,虚拟机和宿主机可以互相访问,但是虚拟机不能访问外网. 需要设置: 1.宿主机设置 先对宿主机(windows机器,我这里是win7系统)进行相关配置. ...
 - C语言程序设计--字符串与指针及数组与指针
			
数组的基本知识 数组的定义 #define SIZE 5 int array_int[5]; //未声明初始化,默认填零 float array_float[5] = {1.01, 2.23, 3.1 ...
 - Android 国内集成使用谷歌地图
			
extends:http://blog.csdn.net/qduningning/article/details/44778751 由于众做周知的原因在国内使用谷歌地图不太方便,在开发中如果直接使用会 ...
 - Java 多线程 ---- 线程中this与 Thread.currentThread()线程的区别
			
总结起来一句话:在Thread中调用this其实就是调用Thread私有Runnable类型的target,target是Thread类的一个属性,而Thread.currentThread()是指新 ...
 - css层叠样式优先级总结
			
虽然学前端最刚开始就是html+css,一直因为这些看起来太简单就没有什么总结,不过看似很简单的东西,研究起来也深不可测,问起来js可能回答的头头是道,css这么简单的东西,其中一个优先级的问题就能难 ...
 - saltstack-----上线环境篇(一)
			
在11上安装mysql yum install mariadb mariadb-server -y 在mysql的配置文件my.cnf中加入 init_connect='SET collation_c ...
 - NuGet 安装EntityFramework5 历程
			
第一步:VS2012中 (据说VS2010还得安装一下NuGet)工具->库程序包管理器->程序包管理器控制台,打开控制台 Install-Package EntityFramework ...
 - Thread和Runable的区别、Synchronized锁关键字
			
一.Thread和Runable的区别 Thread是基类,子类必继承他实现其run方法.其也是实现了Runable接口.Thread是普通的类,并非抽象类或者密封类等. Runnable是接口,子类 ...
 - HDU 2829 - Lawrence - [斜率DP]
			
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 T. E. Lawrence was a controversial figure during ...