前言
力扣上的一系列类似的题目
基本的要求就是找到一个最符合要求的三个数之和(最接近的三数之和),或者一堆符合要求的数组(三数,四数之和),基本免不了遍历整个数组

三数之和
解决思路
就和标题中的一样:排序+双指针,在一个普通遍历里面插入一轮双指针,这个解法比暴力法的时间复杂度会降一个次幂,虽然感觉并没有很大优化,但还是比没有好😂。
题解🚩
废话不多说,直接上代码(代码参考力扣大佬题解)
三数之和

这里加了排序的方法,后面两个就不写了
public class Solution {
public IList<IList<int>> ThreeSum(int[] nums) {
if(nums==null || nums.Length<3) return new List<IList<int>>();
Quick.Sort(nums); //提前排好序
int n=nums.Length;
List<IList<int>> res = new List<IList<int>>();
for(int i=0;i<=nums.Length-3;i++)
{
if(i>0 && nums[i]==nums[i-1]) continue; //重复时就跳过
int a=i+1,b=n-1; //双指针
while(a<b)
{
if(nums[i]+nums[b]+nums[a]>0) b--;
else if (nums[i]+nums[b]+nums[a]<0) a++;
else {
res.Add(new List<int>(new int[]{nums[i],nums[a],nums[b]})); //符合条件,添加到解
while(a<b && nums[a]==nums[a+1]) a++; //跳过重复元素,下同
while(a<b && nums[b]==nums[b-1]) b--;
a++;
b--;
}
}
}
return res;
}
}
public class Quick : Example
{
public static void Sort(int[] items)
{
//TODO:打乱Items数组,消除对输入的依赖
Sort(items, 0, items.Length - 1);
}
public static void Sort(int[] items, int low, int high)
{
if (high <= low) return;
int j = Partition(items, low, high);
Sort(items, low, j - 1);
Sort(items, j + 1, high);
}
private static int Partition(int[] items, int low, int high)
{
int i = low, j = high + 1; //左右扫描指针
IComparable v = items[low]; //切分元素
while (true)
{
//从两头向中间查找,找到符合要求的元素后交换
while (Less(items[++i], v)) if (i == high) break;
while (Less(v, items[--j])) if (j == low) break;
if (i >= j) break;
Exchange(items, i, j);
}
//最后把切分元素插入应处于的位置
Exchange(items, low, j);
return j;
}
}
public class Example
{
protected static bool Less(IComparable item1, IComparable item2)
{
return item1.CompareTo(item2) < 0 && true;
}
protected static void Exchange(int[] items, int i, int j)
{
int temp = items[i];
items[i] = items[j];
items[j] = temp;
}
protected static void Show(IComparable[] items)
{
foreach (IComparable item in items)
{
Console.WriteLine(item.ToString());
}
}
public static bool IsSorted(IComparable[] items)
{
for (int i = 0; i < items.Length - 1; i++)
{
if (items[i].CompareTo(items[i + 1]) > 0)
return false;
}
return true;
}
}
最接近的三数之和

整体来说其实和三数之和没啥变化,因为还是涉及数字比较,那么用双指针基本没错的,甚至这里比一般的三数之和还简单,可以在差为0时直接返回结果。
省略了排序
public class Solution {
public int ThreeSumClosest(int[] nums, int target) {
int min = Int32.MaxValue;
Quick.Sort(nums);
int res=0;
int n = nums.Length;
for(int i=0;i<=n-3;i++)
{
if(i>0 && nums[i]==nums[i-1]) continue; //重复跳过
int a=i+1,b=n-1;
while(a<b)
{
int temp=nums[i]+nums[a]+nums[b];
if(temp<target)
{
a++;
}
else if(temp>target)
{
b--;
}
else{
return temp; //已经得到答案了
}
if(min > Math.Abs(temp-target)) {
min=Math.Abs(temp-target);
res=temp;
}
}
}
return res;
}
}
四数之和

乍一看好像复杂了许多,但实际上和三数之和基本一样,不过是一次遍历+双指针变成了嵌套遍历两次+双指针
public class Solution
{
public IList<IList<int>> FourSum(int[] nums, int target)
{
if (nums == null || nums.Length<4) return new List<IList<int>>();
Quick.Sort(nums);
int n = nums.Length;
List<IList<int>> res = new List<IList<int>>();
for (int a = 0; a <= n - 4; a++)
{
if (a > 0 && nums[a] == nums[a - 1]) continue; //重复跳过,下面b也一样
for (int b = a + 1; b <= n - 3 && b != a; b++)
{
if (b > a + 1 && nums[b] == nums[b - 1]) continue;
int c = b + 1, d = n - 1;
while (c < d)
{
int temp = nums[a] + nums[b] + nums[c] + nums[d];
if (temp < target) c++;
else if (temp > target) d--;
else
{
res.Add(new List<int>(new int[]{nums[a], nums[b], nums[c], nums[d]}));
while (c < d && nums[c] == nums[c + 1]) c++; //重复跳过,下同
while (c < d && nums[d] == nums[d - 1]) d--;
c++;
d--;
}
}
}
}
return res;
}
}

这篇博客介绍了如何利用排序和双指针法解决力扣上的三数之和及最接近的三数之和问题。通过排序优化,将时间复杂度降低,详细阐述了解题思路并提供了C#代码实现。此外,还提及了四数之和问题,尽管看起来更复杂,但解法与三数之和类似,即嵌套遍历与双指针的应用。
1211

被折叠的 条评论
为什么被折叠?



