三数之和
题目描述:
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
来源:力扣(LeetCode)
链接:三数之和
官方题解思路:第一层for循环拆解为两数之和+双指针版的去重两数之和
注意外层不同目标值的二数之和,这个目标值肯定不能重复,不然会出现重复的解!!!!
class Solution {
List<List<Integer>> ans=new ArrayList<List<Integer>>();//寻访最终结果的列表
public List<List<Integer>> threeSum(int[] nums){
//数组长度小于3直接返回
if(nums.length<=2){
return ans;
}
Arrays.sort(nums);//数组一定的要排序
for(int i=0;i<nums.length-2;i++){
if(nums[i]>0){//目标值是个负值,但是后面的数组的值都是正数(直接退出循环)
break;
}
//目标值去重
if(i>0 && nums[i]==nums[i-1]){
continue;//直接进入下一次的循环
}
//开始进行双指针的两数之和
int target=-nums[i];
int left=i+1;
int right=nums.length-1;
while (left<right){//两个指针没有相遇,就没有重复的情况
if(nums[left]+nums[right]==target){
//满足三数之和,直接加入列表中
ans.add(new ArrayList<>(Arrays.asList(nums[i],nums[left],nums[right])));
//开始为下一次的循环做准备,移动指针(左指针右移,右指针左移)--(在等于target的情况下,必然是两个指针都移动,必须同时一个增大一个减小,才能等于target)
while(left<right&&nums[left]==nums[left+1]){
left++;//有重复
}//出循环的right还是上一次出现过的right,但下一个数绝对和它不同
left++;
while(right>left&&nums[right]==nums[right-1]){
right--;//有重复
}
right--;
}else if(nums[left]+nums[right]<target){
left++;//进行微调增加两数之和的值
}else{//nums[right]+nums[left]>target
right--;//进行微调减小两数之和的值
}
}
}
return ans;
}
}
最接近的三数之和
step1:枚举数据a,在a的数据右边利用双指针枚举b和c
step2:针对每一组数据,比较sum和target的值,并更新best即离target最近的值,直到枚举完所有的可能。
优势:相较于三层for循环利用双指针变成了相当于两层循环!!!!
class Solution {
public int threeSumClosest(int[] nums, int target) {
//首先对数组进行排序
Arrays.sort(nums);
int n = nums.length;
int best = 10000000;//初始化为最恶劣的情况
// step1:枚举 a
for (int i = 0; i < n; ++i) {
// 保证和上一次枚举的元素不相等(去重,减少判断)
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
// step2:使用双指针枚举 b 和 c(还是需要穷尽所有的可能)
int j = i + 1, k = n - 1;
while (j < k) {
//当前的三数之和
int sum = nums[i] + nums[j] + nums[k];
// step3.1:如果和为 target 直接返回答案
if (sum == target) {
return target;
}
// step3.2:如果和不为 target,根据差值的绝对值来更新best最接近值
if (Math.abs(sum - target) < Math.abs(best - target)) {
best = sum;
}
//step3.3:比目标值小左指针右移,比目标值大右指针左移,相当于遍历完所有的情况(左右指针每次只有一个指针移动,相当于复杂度O(N))
if (sum > target) {
// 如果和大于 target,移动 c 对应的指针
int k0 = k - 1;
// 移动到下一个不相等的元素(防止重复操作)
while (j < k0 && nums[k0] == nums[k]) {
--k0;
}
k = k0;
} else {
// 如果和小于 target,移动 b 对应的指针
int j0 = j + 1;
// 移动到下一个不相等的元素(防止重复操作)
while (j0 < k && nums[j0] == nums[j]) {
++j0;
}
j = j0;
}
}
}
return best;
}
}
四数之和
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i - 1] == nums[i]) {
continue;
}
for (int j = i + 1; j < nums.length; j++) {
if (j > i + 1 && nums[j - 1] == nums[j]) {
continue;
}
//双指针针对最里面的两重循环
int left = j + 1;
int right = nums.length - 1;
while (right > left) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {//等于目标值时,两边指针都要移动,确保一个增大一个减小才有希望找到新的等于target的值
result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
//防止出现重复--(在等于target的情况下,必然是两个指针都移动,必须同时一个增大一个减小,才能等于target)
while (right > left && nums[right] == nums[right - 1]){
right--;
} //出循环的right还是上一次出现过的right,但下一个数绝对和它不同
right--;
while (right > left && nums[left] == nums[left + 1]){
left++;
}
left++;
}
}
}
}
return result;
}
}