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的更多相关文章

  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 ii k数和 II

    题目: k数和 II 给定n个不同的正整数,整数k(1<= k <= n)以及一个目标数字. 在这n个数里面找出K个数,使得这K个数的和等于目标数字,你需要找出所有满足要求的方案. 样例 ...

  3. Lintcode: Interval Sum II

    Given an integer array in the construct method, implement two methods query(start, end) and modify(i ...

  4. LintCode "k Sum" !!

    Great great DP learning experience:http://www.cnblogs.com/yuzhangcmu/p/4279676.html Remember 2 steps ...

  5. LintCode: Combination Sum II

    C++ DFS class Solution { public: void help(vector<int> &a, int now, int sum, int target, v ...

  6. LintCode "Subarray Sum II"

    Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you ...

  7. k Sum | & ||

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

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

  9. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

随机推荐

  1. Android 全屏Activity以透明的对话框形式弹出

    1. styles.xml <style name="transcutestyle" parent="@android:style/Theme.DeviceDefa ...

  2. Android Usb Camera HAL框架

  3. parted分区脚本

    #!/bin/bash #Used to fomat 6 disks PATH=/bin:/sbin:/usr/bin:/usr/sbin export PATH disk_to_parted=&qu ...

  4. expect小工具,在postgresql中执行sql的shell脚本

    脚本内容: #!/usr/bin/expect set database [lindex $argv 0] set username [lindex $argv 1] set password [li ...

  5. 关于egret.Tween传递参数的问题

    参考: setTimeOut传参数(转) setTimeout传参数 CSDN 一.延迟后执行,并输出参数 for(let i=0;i<10;i++){ egret.Tween.get(this ...

  6. kvm/qemu虚拟机桥接网络创建与配置

    首先阐述一下kvm与qemu的关系,kvm是修改过的qemu,而且使用了硬件支持的仿真,仿真速度比QEMU快. 配置kvm/qemu的网络有两种方法.其一,默认方式为用户模式网络(Usermode N ...

  7. mysql补充(3)优化sql语句查询常用的30种方法

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索 ...

  8. opencv学习之PyImageSearch

    Practical Python and OpenCV+ Case Studies 是一本opencv的入门书籍 强烈推荐一个老外的网站,pyimagesearch 网址:https://www.py ...

  9. 【Python算法】遍历(Traversal)、深度优先(DFS)、广度优先(BFS)

    图结构: 非常强大的结构化思维(或数学)模型.如果您能用图的处理方式来规范化某个问题,即使这个问题本身看上去并不像个图问题,也能使您离解决问题更进一步. 在众多图算法中,我们常会用到一种非常实用的思维 ...

  10. ArcEngine创建要素类_线类型

    public ESRI.ArcGIS.Geodatabase.IFeatureClass CreateFeatureClassForLine(ESRI.ArcGIS.Geodatabase.IWork ...