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 ...
随机推荐
- v-bind小demo
啊哈哈,小颖好久没有更新博客啦,大家有没有想我呀,嘻嘻,自恋一把,
- 【转】.Net+MySQL组合开发 乱码篇
所用工具MySQL5.022VS2005 Team SuiteMySQL Connector Net 5.0.3EMS SQL Manage 2005 For MySQL使用过MySQL的朋友都知道有 ...
- 公钥基础设施体系和EJBCA的一些概念
最近一段时间的在公司做的事情是: 1. 为公司的一些线上系统启用https(使用nginx反向代理的方式来实现,之前的应用无需做改动) 2.为符合规则的用户颁发数字证书(自建CA来实现,目前的用途是给 ...
- 获取Web.config的内容
<web.config> web.config文件是一个XML文件,它的根结点是<configuration>,在<configuration>节点下的常见子节点有 ...
- 23种设计模式之抽象工厂(Abstract Factory)
抽象工厂模式又称为Kit模式,属于对象创建型模式.抽象工厂模式是所有形式的工厂模式中最为抽象和最具一般性的一种形态,它提供了一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类.在抽象工厂模 ...
- 搭建本地DNS解析
一.安装Dnsmasq yum install -y dnsmasq 二.安装dig yum install dnsmasq -y 三.编辑配置文件 vim /etc/dnsmasq.conf res ...
- mysql的启动脚本mysql.server及示例配置文件
以MySQL-server-4.0.14-0.i3862881064151.rpm为例,放在/data目录下 cd /data rpm -ivh MySQL-server-4.0.14-0.i386. ...
- Elasticsearch 基础理论 & 配置调优
一.简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为 ...
- 安装JIRA
参考链接:https://www.cnblogs.com/houchaoying/p/9096118.html mysql-connector插件下载: https://mvnrepository.c ...
- 2018/03/25 每日一个Linux命令 之 df
Linux df命令用于显示目前在Linux系统上的文件系统的磁盘使用情况统计. 就像在windows下打开我的电脑一样会统计各个磁盘一样的情况 主要用于查看磁盘空间占用情况 -- [@hong:~] ...