K Sums
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
All K Sum problem can be divided into two problems:
2 Sum Problem
Reduce K Sum problem to K – 1 Sum Problem
从4Sum和3Sum,我们可以看出对于KSum的通用套路:将KSum转化为K-1 Sum,最后用2Sum的Two Pointer求解。
注意要点:先排序,去除/跳过重复元素
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
return kSum(nums, 0,
public class Solution {
int len = 0;
public List<List<Integer>> fourSum(int[] nums, int target) {
len = nums.length;
Arrays.sort(nums);
return kSum(nums, target, 4, 0);
}
private ArrayList<List<Integer>> kSum(int[] nums, int target, int k, int index) {
ArrayList<List<Integer>> res = new ArrayList<List<Integer>>();
if (index >= len) {
return res;
}
if (k == 2) {
int i = index, j = len - 1;
while (i < j) {
//find a pair
if (target - nums[i] == nums[j]) {
List<Integer> temp = new ArrayList<>();
temp.add(nums[i]);
temp.add(target-nums[i]);
res.add(temp);
i++;
j--;
//skip duplication
while (i < j && nums[i] == nums[i - 1]) i++;
while (i < j && nums[j] == nums[j + 1]) j--;
} else if (target - nums[i] > nums[j]) {
//move left bound
i++;
} else {
//move right bound
j--;
}
}
} else {
for (int i = index; i < len - k + 1; i++) {
// Alternative: Skipping duplicates in the front
// if (i > index && nums[i] == nums[i - 1]) {
// continue;
// }
//use current number to reduce k sum into k-1 sum
ArrayList<List<Integer>> temp = kSum(nums, target - nums[i], k - 1, i + 1);
if (temp != null) {
//add previous results
for (List<Integer> t : temp) {
t.add(0, nums[i]);
}
res.addAll(temp);
}
//skip duplicated numbers
while (i < len - 1 && nums[i] == nums[i + 1]) {
i++;
}
}
}
return res;
}
}
Generalized KSums: https://leetcode.com/problems/4sum/discuss/8609/My-solution-generalized-for-kSums-in-JAVA