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 ...
随机推荐
- Android.mk(5) 计算怎么办?
https://www.jianshu.com/p/57c01e97c9b8 计算怎么办? 前面我们把Makefile做为一门语言的主要特性大致做了一个描述,它集合了目标式的模式和函数式的模式,还有大 ...
- linux系统中关于shell变量$*与$@的区别
在我们初学linux系统shell时,可能会感觉$@与$*没什么区别,如下面shell脚本: #!/bin/bash# name:a.sh # echo 'this script $* is: '$* ...
- iOS8新特性(1)——UIAlertController
一.iOS8介绍 iOS8 新特性,主要是UI上进行了统一 1.UIAlertController 2.UIPresentaionController:管理所有通过modal出来的控制器(看笔记) 3 ...
- EasyUI常用控件禁用方法
EasyUI常用控件禁用方法: 1.validatebox可以用的用法:前两种适用于单个的validatebox; 第三种应用于整个form里面的输入框; <1>.$("#id& ...
- Java实现验证码的产生和验证
大家都知道为了防止我们的网站被有些人和黑客恶意攻击,比如我们网站的注册页面,如果我们在用户注册的时候不加上一个验证码框的话,别人就可以写一个脚本对你的网站进行恶意的注册,比如每分钟对你的网站进行n次的 ...
- 【转】(翻译)从底层了解ASP.NET体系结构
原文地址:http://www.cnblogs.com/rijing2004/archive/2007/09/14/howaspnetwork.html 前言关于ASP.NET的底层的工作机制,最近园 ...
- codeforces 894B - Ralph And His Magic Field - [数学题]
题目链接:https://cn.vjudge.net/problem/CodeForces-894B Ralph has a magic field which is divided into n × ...
- easyui-layout个人实例
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 【作业】 iterator,set_union 一些奇怪的语法
关于set_union系列函数(需要有序)的第五个参数,output iterator. 网上都是用inserter(c,c.begin()) 但vs会编译报错 所以改成了back_inserter, ...
- Oracle体系结构之数据文件管理
数据文件分2个方向管理: 物理结构和逻辑结构. 数据库的存储层次结构图: ............. 逻辑结构: 物理结构: .... ...