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. [LeetCode][Python]18: 4Sum

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

  2. Qt学习之路(49): 通用算法

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

  3. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

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

  4. 1. Two Sum&&15. 3Sum&&18. 4Sum

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

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

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

  6. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. 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. 3Sum、16. 3Sum Closest和18. 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. 4Sum (JAVA)

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

随机推荐

  1. JEECG 新手常见问题大全,入门必读

    大家还有什么问题,请跟帖,谢谢支持..  JEECG常见问题大全 原文地址:http://t.cn/RvYsEF61. jeecg数据库脚本问题   注意:jeecg3.5.2之前版本,不需要数据库脚 ...

  2. java web项目使用IDEA打成war包

    步骤: 1.点击 File -->Project Structure...如下图: 2.出现如下界面后点击 Artifacts--> 绿色加号-->Web Application:A ...

  3. 关于 C++ 默认构造函数 的几个误区 转载

    https://blog.csdn.net/ccrazyman/article/details/8138425

  4. ORM一对多查询对象

    正向查询: 取人民日报出版社出版的所有书籍 方式一: pub_obj = Publish.objects.filter(name='人民日报')[0] ret = Book.objects.filte ...

  5. js 提示框的实现---组件开发之(一)

    自己做了一个简单的提示框,供自己使用,也可以供他人参考,看懂此文,是理解组件开发的入门 思路比较简单: 1.常规写法: 1.1. 创建一个构造函数 1.2. 给构造函数的原型对象添加show 和hid ...

  6. Servlet基本_クッキー、URLリライティング

    1.クッキーの基礎クッキーは.クライアント側に保存されるテキストデータです. セキュリティ上の制約.・自分で発行したクッキーにしかアクセスできない.クッキーには発行元のホストの情報が記録されている.・ ...

  7. delphi异常捕获try except语句 和 try finally语句用法

    原文地址:delphi try except语句 和 try finally语句用法以及区别作者:1865898133 一直写程序都没管他们,也尽量很少用,今天终于想把他给弄个明白,在网上找来,记下! ...

  8. TypeScript语言学习笔记(2)

    接口 // 在参数类型中定义约束 function printLabel(labelledObj: { label: string }) { console.log(labelledObj.label ...

  9. Redis进阶实践之六Redis Desktop Manager连接Windows和Linux系统上的Redis服务(转载6)

    Redis进阶实践之六Redis Desktop Manager连接Windows和Linux系统上的Redis服务 一.引言 今天本来没有打算写这篇文章,但是,今天测试Redis的时候发现了两个问题 ...

  10. 15.过滤器-基础.md

    目录 基础 实例 图解 核心API interface Filter过滤器接口 interface FilterConfig获取过滤器初始化信息 interface FilterChain 过滤器参数 ...