18. 4Sum (通用算法 nSum)
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)的更多相关文章
- [LeetCode][Python]18: 4Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...
- Qt学习之路(49): 通用算法
今天开始的部分是关于Qt提供的一些通用算法.这部分内容来自C++ GUI Programming with Qt 4, 2nd Edition. <QtAlgorithms>提供了一系 ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- 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 ...
- 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), ...
- 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 ...
- Qt容器类之三:通用算法
在<QtAlgorithm>头文件中,Qt提供了一些全局的模板函数,这些函数是可以使用在容器上的十分常用的算法.我们可以在任何提供了STL风格迭代器的容器类上用这些算法,包括QList.Q ...
- 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 = ...
- 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 ...
随机推荐
- VS2017创建一个 ASP.NET Core2.0 应用,并搭建 MVC 框架
https://testerhome.com/topics/11747 1.使用最新版本的VS2017,并安装.NET Core2.0中相关开发工具 2.打开VS2017,点击文件-新建-项目,选 ...
- GitHub创建个人主页
在GitHub,一个项目对应唯一的Git版本库,创建一个新的版本库就是创建一个新的项目.访问仪表板(Dashboard)页面,如图3-1,可以看 到关注的版本库中已经有一个,但自己的版本库为零.在显示 ...
- js 编写一个神奇的四则运算
写一个算法,有时候可以用简单的方法就可以写出来,但是只能针对特定的环境,如果要能够适应不同的环境,就需要对算法进行优化,在优化的过程中,你会觉得非常神奇,下面来看一个简单的四则运算的算法编写方式: 1 ...
- Mysql(MyISAM和InnoDB)及Btree和索引优化
MYSQL 一.引擎 mysql:MySQL是一个关系型数据库管理系统,其中有两种引擎最为常见MyISAM和InnoDB MyISAM(非聚集索引) MySQL 5.0 之前的默认数据库引擎,最为常 ...
- thinkphp的PHPexecl表格样式设置
/** * 以下是使用示例,对于以 //// 开头的行是不同的可选方式,请根据实际需要 * 打开对应行的注释. * 如果使用 Excel5 ,输出的内容应该是GBK编码. */ //require_o ...
- C# WINFORM 打包数据库
实现效果:安装项目时直接附加数据库. 1.首先在需要部署的项目的解决方案资源管理器中新建一个安装项目 2.在安装项目的文件视图中,右键[应用程序文件夹]->[添加]->[项目输出] ...
- 记Dagger2使用过程中的一个BUG--compileGoogleDebugJavaWithJavac
项目编译可以通过,不过没有生成Dagger2的类,导致无法运行项目.. 错误提示 Error:(14, 41) 错误: 找不到符号 符号: 类 DaggerAppComponent 位置: 程序包 c ...
- CFDA
cfda数据抓取 1.网站数据是加密的,需要浏览器进行数据解析 2.网址url有js加密 3.PhantomJS无法解析数据, chrome无法获取数据,所有最终选择用Firefox浏览器 impor ...
- 学JS的心路历程-JS支持面向对象?(一)
昨天在看Prototype看到JS支持面向对象,被前辈问到说那什么是面向对象?JS是面向对象语言吗? 便开始了一连串艰辛爬文过程,今天就来看一下两者有什么差异吧(rgwyjc)! 首先面向对象有三大特 ...
- 关于ubuntu14.04,忘记root密码解决方案(经测试,内核3.13和4.2可行)。
网上已经有很多方案啦,我这里就不画蛇添足,直接上链接: http://jingyan.baidu.com/article/c843ea0b9e851077931e4aea.html 注意,有些机子没有 ...