# Leetcode 3Sum Closest和4Sum问题

## 16. 3Sum Closest

### 题目描述

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

### 一般做法

class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
//与target的最小差
int cha=Integer.MAX_VALUE;
int ans=0;
for (int i=0;i<nums.length-2;i++)
{
if(i>0&&nums[i]==nums[i-1]) continue;
int left=i+1,right=nums.length-1;
while(left<right)
{
int x=nums[i]+nums[left]+nums[right];
if(cha>Math.abs(x-target))
{
cha=Math.abs(x-target);
ans=x;
}
if(x==target)
{
ans=target;
break;
}
else if(x>target)
{
right--;
}
else
{
left++;
}

}
if(ans==target) break;
}
return ans;
}
}


### 一般做法的优化

class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int cha=Integer.MAX_VALUE;
int ans=0;
for (int i=0;i<nums.length-2;i++)
{
if(i>0&&nums[i]==nums[i-1]) continue;
int left=i+1,right=nums.length-1;
int tar_cha=target-nums[i];
int Max=nums[i]+nums[nums.length-1]+nums[nums.length-2];
//两条优化
if(Max==target)
{
ans=target;
break;
}
else if(Max<target)
{
if(cha>target-Max)
{
cha=target-Max;
ans=Max;
}
continue;
}
while(left<right)
{
int x=nums[i]+nums[left]+nums[right];
if(cha>Math.abs(x-target))
{
cha=Math.abs(x-target);
ans=x;
}
if(x==target)
{
ans=target;
break;
}
else if(x>target) right--;
else left++;

}
if(ans==target) break;
}
return ans;
}
}


## 18. 4Sum

### 题目描述

Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

The solution set must not contain duplicate quadruplets.

Example:

Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]

### 一般做法

class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
Arrays.sort(nums);
for (int i=0;i<nums.length-3;i++)
{
if(i>0&&nums[i]==nums[i-1]) continue;
for (int j=i+1;j<nums.length-2;j++)
{
if(target>0&&nums[i]+nums[j]>target) break;
if(j>i+1&&nums[j]==nums[j-1]) continue;
int tar_cha = target-nums[j]-nums[i];
int left=j+1,right=nums.length-1;
while(left<right)
{
int x = nums[left]+nums[right];
if(x==tar_cha)
{
left++;right--;
while(left<right&&nums[left]==nums[left-1]) left++;
while(left<right&&nums[right]==nums[right+1]) right--;
}
else if(x>tar_cha) right--;
else left++;
}
}
}
return list;
}
}


### 一般做法的优化

1.在选完第一个数a后，如果a+nums中的最后一个数3小于target的话，直接continue执行下一层
2.在选完第一个数a后，当4
a大于target的话，直接跳出循环
3.选完a和b后，如果a+b+nums最后一个数2小于target，直接continue执行下一层
4.选完a和b后，如果a+3
b大于target，直接跳出循环

class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
Arrays.sort(nums);
for (int i=0;i<nums.length-3;i++)
{
if(i>0&&nums[i]==nums[i-1]) continue;
// 1,2条判断规则
if(nums[i]+3*nums[nums.length-1]<target) continue;
if(nums[i]*4>target) break;
for (int j=i+1;j<nums.length-2;j++)
{
if(j>i+1&&nums[j]==nums[j-1]) continue;
//3,4条判断规则
if(nums[i]+nums[j]+nums[nums.length-1]*2<target) continue;
if(nums[i]+nums[j]*3>target) break;
int tar_cha = target-nums[j]-nums[i];
int left=j+1,right=nums.length-1;
while(left<right)
{
int x = nums[left]+nums[right];
if(x==tar_cha)
{
left++;right--;
while(left<right&&nums[left]==nums[left-1]) left++;
while(left<right&&nums[right]==nums[right+1]) right--;
}
else if(x>tar_cha) right--;
else left++;
}
}
}
return list;
}
}


©️2019 CSDN 皮肤主题: 撸撸猫 设计师: 设计师小姐姐