18. 4Sum (通用算法 nSum)

时间:2022-06-06 08:46:52

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

Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
For example, given array S = {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)

/**
* Return an array of arrays of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/ int** fourSum(int* nums, int numsSize, int target, int* returnSize) {
quickSort(nums, , numsSize-); int* elem = malloc(sizeof(int)*);
int** returnArray = malloc(sizeof(int*)*);
nSum(nums, numsSize, target, elem, returnArray, returnSize, );
return returnArray;
} void twoSum(int* nums, int numsSize, int target, int* elem, int** returnArray, int* returnSize){
int j = ;
int k = numsSize-;
while(j<k){
if(nums[j]+nums[k] < target) j++;
else if(nums[j]+nums[k] > target) k--;
else{
elem[] = nums[j];
elem[] = nums[k]; int* returnElem = malloc(sizeof(int)*);
memcpy(returnElem, elem,sizeof(int)*); returnArray[*returnSize] = returnElem;
(*returnSize)++; j++;
k--;
while(j<k && nums[j]==nums[j-]) j++; //To avoid duplicate triplets
while(j<k && nums[k]==nums[k+]) k--; }
}
} void nSum(int* nums, int numsSize, int target, int* elem, int** returnArray, int* returnSize, int N){
if(N<=) {
twoSum(nums, numsSize, target, elem, returnArray, returnSize);
return;
} N--;
for(int i = ; i < numsSize-N; i++){
elem[-N-] = nums[i];
nSum(nums+i+, numsSize-i-, target-nums[i], elem, returnArray, returnSize, N);
while(nums[i+]==nums[i]) i++; //To avoid duplicate triplets
}
} void quickSort(int* nums, int start, int end){
int p1 = start+;
int p2 = end;
int tmp; while(p1 <= p2){
while(p1 <= p2 && nums[p1] <= nums[start]){
p1++;
}
while(p1 <= p2 && nums[p2] > nums[start]){
p2--;
}
if(p1 < p2){
tmp = nums[p1];
nums[p1] = nums[p2];
nums[p2] = tmp;
p1++;
p2--;
}
} //put the sentinel at the end of the first subarray
if(start!=p2){
tmp = nums[start];
nums[start] = nums[p2];
nums[p2] = tmp;
} if(start < p2-) quickSort(nums,start, p2-); //sort first subarray (<=sentinel)
if(p1 < end) quickSort(nums,p1, end); //sort second subarray (>sentinel)
}

18. 4Sum (通用算法 nSum)的更多相关文章

  1. &lbrack;LeetCode&rsqb;&lbrack;Python&rsqb;18&colon; 4Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...

  2. Qt学习之路&lpar;49&rpar;&colon; 通用算法

    今天开始的部分是关于Qt提供的一些通用算法.这部分内容来自C++ GUI Programming with Qt 4, 2nd Edition.   <QtAlgorithms>提供了一系 ...

  3. LeetCode 15&period; 3Sum 16&period; 3Sum Closest 18&period; 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  4. 1&period; Two Sum&amp&semi;&amp&semi;15&period; 3Sum&amp&semi;&amp&semi;18&period; 4Sum

    题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up t ...

  5. 1&period; Two Sum &plus; 15&period; 3 Sum &plus; 16&period; 3 Sum Closest &plus; 18&period; 4Sum &plus; 167&period; Two Sum II - Input array is sorted &plus; 454&period; 4Sum II &plus; 653&period; Two Sum IV - Input is a BST

    ▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...

  6. leetcode 1&period;Two Sum 、167&period; Two Sum II - Input array is sorted 、15&period; 3Sum 、16&period; 3Sum Closest 、 18&period; 4Sum 、653&period; Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  7. Qt容器类之三:通用算法

    在<QtAlgorithm>头文件中,Qt提供了一些全局的模板函数,这些函数是可以使用在容器上的十分常用的算法.我们可以在任何提供了STL风格迭代器的容器类上用这些算法,包括QList.Q ...

  8. 15&period; 3Sum、16&period; 3Sum Closest和18&period; 4Sum

    15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...

  9. 18&period; 4Sum &lpar;JAVA&rpar;

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

随机推荐

  1. docker develop django

    $ docker pull django $ docker run -it -v $(pwd):/usr/src/app -p 8000:8000 django /bin/bash

  2. 【BO】SAP BO相关问题汇总贴

    本文将以往写过的关于SAP BO相关问题的帖子汇总了一下,方便寻找 #1 为WEBI报表添加自定义字体font #2 WEBI文件打开时提示Illegal access错误 #3 安装BO服务器时,o ...

  3. MA均线组合

    MA5.MA13.MA21.MA34.MA55.MA90.MA120.MA250

  4. Spark Streaming 入门指南

    这篇博客帮你开始使用Apache Spark Streaming和HBase.Spark Streaming是核心Spark API的一个扩展,它能够处理连续数据流. Spark Streaming是 ...

  5. 欢迎使用IdentityModel文档!- IdentityModel 中文文档&lpar;v1&period;0&period;0&rpar;

    IdentityModel是基于声明的身份,OAuth 2.0和OpenID Connect的.NET标准帮助程序库. 它具有以下高级功能: 标准OAuth 2.0和OpenID Connect端点的 ...

  6. 如何用思维导图快速理解PMBOK-PMP第六版教材

    PMP的教材很多人拿到PMBOK的时候都觉得头疼,这书本这么厚,我什么时候能看完啊,确实是,没有体系,没有框架的看书是很难的,看的很多知识点都无法联系起来.所以利用思维导图来学习PMP就很有效果,下面 ...

  7. Java基础之IO流学习总结

    Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  8. ERROR 1067 &lpar;42000&rpar;&colon; Invalid default value for &&num;39&semi;created&lowbar;time&&num;39&semi;【转】

    执行表增加字段语句报错 mysql> ALTER TABLE ha_question ADD COLUMN question_number INT; ERROR (): Invalid defa ...

  9. 11&period;1 正睿停课训练 Day14

    目录 2018.11.1 正睿停课训练 Day14 A 字符串 B 取数游戏(贪心) C 魔方(模拟) 考试代码 B C 2018.11.1 正睿停课训练 Day14 时间:3.5h 期望得分:100 ...

  10. Python笔记(十三):urllib模块

    (一)      URL地址 URL地址组件 URL组件 说明 scheme 网络协议或下载方案 net_loc 服务器所在地(也许含有用户信息) path 使用(/)分割的文件或CGI应用的路径 p ...