List<List<Integer>> kSum_Trim(int[] a, int target, int k) {
List<List<Integer>> result = new ArrayList<>();
if (a == null || a.length < k || k < 2) return result;
Arrays.sort(a);
kSum_Trim(a, target, k, 0, result, new ArrayList<>());
return result;
} void kSum_Trim(int[] a, int target, int k, int start, List<List<Integer>> result, List<Integer> path) {
int max = a[a.length - 1];
if (a[start] * k > target || max * k < target) return; if (k == 2) { // 2 Sum
int left = start;
int right = a.length - 1;
while (left < right) {
if (a[left] + a[right] < target) left++;
else if (a[left] + a[right] > target) right--;
else {
result.add(new ArrayList<>(path));
result.get(result.size() - 1).addAll(Arrays.asList(a[left], a[right]));
left++; right--;
while (left < right && a[left] == a[left - 1]) left++;
while (left < right && a[right] == a[right + 1]) right--;
}
}
}
else { // k Sum
for (int i = start; i < a.length - k + 1; i++) {
if (i > start && a[i] == a[i - 1]) continue;
if (a[i] + max * (k - 1) < target) continue;
if (a[i] * k > target) break;
if (a[i] * k == target) {
if (a[i + k - 1] == a[i]) {
result.add(new ArrayList<>(path));
List<Integer> temp = new ArrayList<>();
for (int x = 0; x < k; x++) temp.add(a[i]);
result.get(result.size() - 1).addAll(temp); // Add result immediately.
}
break;
}
path.add(a[i]);
kSum_Trim(a, target - a[i], k - 1, i + 1, result, path);
path.remove(path.size() - 1); // Backtracking
}
}
}

Leetcode - K Sum的更多相关文章

  1. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  2. summary of k Sum problem and solutions in leetcode

    I found summary of k Sum problem and solutions in leetcode on the Internet. http://www.sigmainfy.com ...

  3. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  4. LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结

    前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...

  5. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  6. lintcode: k Sum 解题报告

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

  7. k sum 问题系列

    转自:http://tech-wonderland.net/blog/summary-of-ksum-problems.html (中文旧版)前言: 做过leetcode的人都知道, 里面有2sum, ...

  8. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  9. k Sum | & ||

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

随机推荐

  1. JDK8新特性之方法引用

    什么是方法引用 方法引用是只需要使用方法的名字,而具体调用交给函数式接口,需要和Lambda表达式配合使用. 如: List<String> list = Arrays.asList(&q ...

  2. D-Ubuntu中修改MySQL的默认数据集(client和server)

    Ubuntu16.04,MySQL5.7 1, sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf 使用vim编辑MySQL的配置文件,不同版本的MySQL配置文件 ...

  3. IPointCollection.AddPoint Method的用法

    补充指出官方帮助的一个问题. 该方法用于向环.线.面等几何中添加节点.官方帮助里是这样描述的:Adds a vertex to a Path, Ring, Polyline, or Polygon; ...

  4. python_learn Ⅰ

    基于 廖雪峰python3教程 学习. 目录: 01_输入输出.py 02_list.tuple.py 03_条件判断.py 04_循环.py 05_利用循环排序.py 06_自定义3元2次方程的根求 ...

  5. 2019-9-25-如何让-USB-设备不显示安全删除硬件弹出选项

    title author date CreateTime categories 如何让 USB 设备不显示安全删除硬件弹出选项 lindexi 2019-09-25 11:58:19 +0800 20 ...

  6. PushSharp 由于远程方已关闭传输流,身份验证失败。

    前段时间用到了PushSharp给APNS发推送,但是用的时候遇见很诡异的事情,每次第一次运行的时候能成功发送到 但是接下来就无限的提示“由于远程方已关闭传输流,身份验证失败. “ 然后我就各种找原因 ...

  7. 第二天:PowerShell别名

    1.查询别名: Get-Alias -name ls Get-Alias -name dir Get-Alias -name fl Get-Alias -name ft 2.查看可用的别名 查看可用的 ...

  8. __init__初始化方法

    使用场景:多个对象(由同一个类产生)的属性同名且值都一样,这时就需要使用init()方法. # 多个对象(由同一个类产生)的属性同名且值都一样,这时就需要使用__init__()方法. # class ...

  9. Andriod Unity 调用android函数

    //首先这是一个前沿 //我看了多的资料,还看了近半个小时的 android activity 的讲解 终于打出了apk //接下来开始 //一步一步的跟着我走 1.创建一个android项目或者一个 ...

  10. Unity UGUI和特效(含粒子系统和3D Object)之间层级问题

    撰写本文时使用Unity的版本为2017.4.10f1(64bit) 1.描述问题 需求:通过UGUI制作一个界面之后,我需要在界面的后面跟前面各添加一个特效. 问题:同一层级UI是最后渲染的,所以U ...