LeetCode18四数之和

代码来源:代码随想录

c 复制代码
/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int compare(const void *a,const void *b)
{
    int a1=*(int *)a;
    int b1=*(int *)b;
    return (a1>b1)-(a1<b1);
}
int** fourSum(int* nums, int numsSize, int target, int* returnSize, int** returnColumnSizes)
{
    //1.对数组进行排序
    qsort(nums,numsSize,sizeof(int),compare);
    //2.分配空间,解的数量上限是C(200 4),但经验值为40000,避免过度分配内存
    int **result=(int **)malloc(sizeof(int *)*40000);
    int count=0;//计数器,记录解的数量
    for(int i=0;i<numsSize-3;i++)
    {
        /* 剪枝:如果nums[i]已经大于target且为正数,后面不可能有解 */
        if ((nums[i] > target) && (nums[i] >= 0)) {
            break;
        }
        /* 去重:跳过重复的nums[i] */
        if ((i > 0) && (nums[i] == nums[i - 1])) {
            continue;
        }
        for(int j=i+1;j<numsSize-2;j++)
        {
            /* 剪枝:如果前两数之和已经大于target且nums[j]为正数 */
            if ((nums[i] + nums[j] > target) && (nums[j] >= 0)) {
                break;
            }
            /* 去重:跳过重复的nums[i] */
            if ((j > (i + 1)) && (nums[j] == nums[j - 1])) {
                continue;
            }
            //左指针
            int left=j+1;
            //右指针
            int right=numsSize-1;
            while(left<right)
            {
                long long sum=(long long)nums[i]+nums[j]+nums[left]+nums[right];
                if(sum>target)
                {
                    right--;
                }
                else if(sum<target)
                {
                    left++;
                }
                else
                {
                    //3.找到符合条件的四元组记录到result中
                    int *result_tmp=(int *)malloc(sizeof(int)*4);
                    result_tmp[0]=nums[i];
                    result_tmp[1]=nums[j];
                    result_tmp[2]=nums[left];
                    result_tmp[3]=nums[right];
                    result[count++]=result_tmp;
                    //去重,去掉重复的left和right
                    while ((right > left) && (nums[right] == nums[right - 1])) {
                        right--;
                    }
                    while ((left < right) && (nums[left] == nums[left + 1])) {
                        left++;
                    }
                    right--;
                    left++;
                }
            }
        }
    }
    *returnSize=count;
    int *column=(int *)malloc(sizeof(int)*count);
    for(int i=0;i<count;i++)
    {
        column[i]=4;
    }
    *returnColumnSizes=column;
    return result;
}
相关推荐
SomeB1oody3 分钟前
【RustyML入门】6.3. 并行归约
开发语言·后端·机器学习·rust·教程
东小西23 分钟前
【SAA实战】第 1 篇:ReactAgent 入门——先撸个"会调工具的助手"跑起来
java·人工智能·spring
兴通物联科技40 分钟前
SMT PCB 微小 DataMatrix 码扫不动问题分析 兴通 XT8601B 600 万像素工业读码器落地实践
大数据·人工智能·单片机·嵌入式硬件·算法·计算机视觉
mqiqe1 小时前
AgentScope Java 2.0 协议集成全景解析:A2A、AG-UI、Agent Protocol 三大开放协议实战指南
java·开发语言·ui
lzhdim1 小时前
12、JavaScript常见的内存泄露问题 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
脉动数据行情1 小时前
Java 实现台股 TWSE/TPEx 行情采集(个股 + K 线)
java·开发语言·twse·tpex·台股
爱学习的小邓同学1 小时前
Golang --- (1)第一个Golang程序
开发语言·后端·golang
zx_741484812 小时前
【Python 入门】面向对象基础:类、对象、成员变量与构造方法
开发语言·python
月华路3 小时前
G1 GC 对数组与大对象(Humongous)的处理
java·jvm·算法