给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
注意:
答案中不可以包含重复的四元组。
示例:
给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。
满足要求的四元组集合为:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/4sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
审题:
判断四数之和是等于合给定结果,而且不能有重复。和上前几题,三数之和有些类似。
思考:
三数之和可以双向移动,四数之和双向移动需要再思考新的思路。
解题:
方法一:
// 第一种方法,思路是三数之和循环两次。
public List<List<Integer>> fourSum_1(int[] nums, int target) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
Arrays.sort(nums);
// //首先判断数组长度大于四个元素
if (nums.length >= 4) {
// 第一次遍历
for (int i = 0; i < nums.length - 3; i++) {
// 去除重复的
// if (i == 0 || (i > 0 && nums[i] == nums[i - 1])) {
// 第二个循环
for (int j = i + 1; j < nums.length - 2; j++) {
// if (j == i + 1 || (j > i + 1 && nums[j] == nums[j - 1])) {
int left = j + 1;
int right = nums.length - 1;
int sum = target - nums[i] - nums[j];
while (left < right) {
if (sum == (nums[left] + nums[right])) {
// 是想要的结果
list.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
// 如果左边相等,下一个
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
// 右边
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
left++;
right--;
} else if (sum > (nums[left] + nums[right])) {
left++;
} else {
right--;
}
}
}
}
}
return list;
}
方法二:与方法一思想是一样的,不过有优化。
public List<List<Integer>> fourSum_3(int[] nums, int target) {
ArrayList<List<Integer>> res = new ArrayList<List<Integer>>();
// 数组的长度
int len = nums.length;
// 如果数组长度小于4直接返回
if (nums != null || len < 4) {
return res;
}
// 排序
Arrays.sort(nums);
int max = nums[len - 1];
// 如果最大的数字乘以4小于和,或者最小的数乘以4大于和。都不合适。
if (nums[0] * 4 > target || max * 4 < target) {
return res;
}
int i, z;
for (i = 0; i < len - 1; i++) {
z = nums[i];
// 如果重复,跳出继续循环下一个。
if (i > 0 && z == nums[i - 1]) {
continue;
}
// 第一个(最小的数字),乘以四还是大于target.直接结束,返回res.
if (z * 4 > target) {
break;
}
// 小于,第一个数加最后三个数,还是太小,循环下一个数子
if (z + max * 3 < target) {
continue;
}
if (z * 4 == target) {
if (i + 3 < len && z == nums[i + 3]) {
res.add(Arrays.asList(z, z, z, z));
}
break;
}
// 如果走到这步,说明第一个数字已经选择出来。
// nums数组;,target-z三数之和;i+1下一个循环的数;len-1最右;res数组;z第一个数字,
threeSumForFourSum_2(nums, target, i + 1, len - 1, res, z);
}
return res;
}
// 寻找第二个数字。逻辑和第一个一样。
private void threeSumForFourSum_2(int[] nums, int target, int low, int high, ArrayList<List<Integer>> res, int z1) {
if (low + 1 >= high) {
return;
}
int max = nums[high];
if (3 * nums[low] > target || 3 * max < target)
return;
int i, z;
for (i = low; i < high - 1; i++) {
z = nums[i];
if (i > low && z == nums[i - 1]) // avoid duplicate
continue;
if (z + 2 * max < target) // z is too small
continue;
if (3 * z > target) // z is too large
break;
if (3 * z == target) { // z is the boundary
if (i + 1 < high && nums[i + 2] == z)
res.add(Arrays.asList(z1, z, z, z));
break;
}
twoSumForFourSum_2(nums, target - z, i + 1, high, res, z1, z);
}
}
// 剩下的两个数,求两数之和
private void twoSumForFourSum_2(int[] nums, int target, int low, int high, ArrayList<List<Integer>> res, int z1,
int z) {
if (low >= high) {
return;
}
// 判断两边的极端情况
if (nums[low] * 2 > target || nums[high] < target) {
return;
}
while (low < high) {
int num = nums[low] + nums[high];
if (num == target) {
res.add(Arrays.asList(z1, z, nums[low], nums[high]));
int x = nums[low];
while (low++ < high && x == nums[low])
;
x = nums[high];
while (low < high-- && x == nums[high])
;
}
if (num < target)
low++;
if (num > target)
high--;
}
return;
}
知识点:
重要的思想是,分而治之。一个一个找,最后合起来,在找第一个的时候需要做很多判断,保证这个符合要求。