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

Note:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4},

A solution set is:
(-1, 0, 1)
(-1, -1, 2)

/**
* Return an array of arrays of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int** threeSum(int* nums, int numsSize, int* returnSize) {
int target;
int i = , j, k;
int *solutionSet; //element in the returnArray
int** returnArray = NULL;
int size = ; //size of returnArray quickSort(nums, , numsSize-); for(; i < numsSize-; i++){ //最外层遍历每个元素
target = - nums[i]; //里层: Two Sum
j = i+;
k = numsSize-;
while(j<k){
if(nums[j]+nums[k] < target) j++;
else if(nums[j]+nums[k] > target) k--;
else{
solutionSet = malloc(sizeof(int)*);
solutionSet[] = nums[i];
solutionSet[] = nums[j];
solutionSet[] = nums[k]; j++;
k--;
size++; returnArray = realloc(returnArray,size*sizeof(solutionSet));
returnArray[size-] = solutionSet; while(j<k && nums[j]==nums[j-]) j++; //To avoid duplicate triplets
while(j<k && nums[k]==nums[k+]) k--; }
}
while(i<numsSize- && nums[i]==nums[i+]) i++;//To avoid duplicate triplets
}
*returnSize = size;
return returnArray;
} 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)
}

15. 3Sum (重新分配数组大小)的更多相关文章

  1. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  2. C++中delete[]是如何知道数组大小的

    先看一段代码: int main(void) { int *pI = new int; int *pArray = new int[10]; int size = *(pArray-1); delet ...

  3. c++: 获取delete[]中的数组大小

    看一个小例子: 1 #include <iostream> 2   3 using namespace std; 4   5 class A { 6 public: 7     A() { ...

  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. 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 ...

  6. 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 = ...

  7. C++ 数组作为函数参数时,传递数组大小的方法

    废话不多说,先上错误示范: void fun(int arr[arr_num]) { // ... } int main() { // ... int *arr = new int[10]; fun( ...

  8. ylbtech-Java-Runoob-高级教程-实例-数组:15. Java 实例 – 判断数组是否相等

    ylbtech-Java-Runoob-高级教程-实例-数组:15. Java 实例 – 判断数组是否相等 1.返回顶部 1. Java 实例 - 判断数组是否相等  Java 实例 以下实例演示了如 ...

  9. 将一组数组向右移动k位,末尾的要转置移动到数组开始,其中n为数组大小,0<k<n

    下面是使用a数组本身完成: package 数组元素k位右移; /** * 数组向又移动k位. 0<k<n * * @author SeeClanUkyo 将一组数组向右移动k位,末尾的要 ...

随机推荐

  1. js判断用户是客户端还是移动端

    js判断用户是客户端还是移动端 Javascript 判断客户端是否为 PC 还是手持设备,有时候项目中需要用到,很方便的源生检测,方法一共有两种   1.第一种: function IsPC() { ...

  2. 机器学习进阶-图像金字塔与轮廓检测-轮廓检测 1.cv2.cvtColor(图像颜色转换) 2.cv2.findContours(找出图像的轮廓) 3.cv2.drawContours(画出图像轮廓) 4.cv2.contourArea(轮廓面积) 5.cv2.arcLength(轮廓周长) 6.cv2.aprroxPloyDP(获得轮廓近似) 7.cv2.boudingrect(外接圆)..

    1. cv2.cvtcolor(img, cv2.COLOR_BGR2GRAY) # 将彩色图转换为灰度图 参数说明: img表示输入的图片, cv2.COLOR_BGR2GRAY表示颜色的变换形式 ...

  3. python 阿狸的进阶之路(4)

    装饰器 #1.开放封闭原则:对扩展开放,对修改是封闭#2.装饰器:装饰它人的,器指的是任意可调用对象,现在的场景装饰器->函数,被装饰的对象也是->函数#原则:1.不修改被装饰对象的源代码 ...

  4. rocketmq 4.2.0 版本 控制台本地搭建(史上最简单教程)

    就像发现新大陆一般,瞎折腾,搞出来了..并没有网上说的一大串....(本人公司的项目从未使用过springboot....) rocketmq  控制台,官方使用springboot 做后端,前端使用 ...

  5. CSS 的 ID 和 Class 有什么区别,如何正确使用它们。

    css只用class来写并有专门的class通用和私有模块命名, id具有唯一性且优先级太高只作为js操作dom的挂钩全部不添加样式, 如果使用jq或zepto的话,操作的class类名一般也不加样式 ...

  6. Hbase数据读写流程

    From: https://blog.csdn.net/wuxintdrh/article/details/69056188 写操作: Client写入,存入Memstore,Memstore满则Fl ...

  7. Python文件和目录模块介绍:glob、shutil、ConfigParser

    glob模块 查找符合特定规则的文件路径名,路径名可以使用绝对路径也可以使用相对路径.查找文件会使用到三个通配符,星号*,问号?和中括号[],其中"*"表示匹配0~n个字符, &q ...

  8. [jQ]jQuery显式操作Checkbox,并用数组存储关联值的方案

    ---------------------------------------------------------------------------------------------------- ...

  9. iframe多窗口

    Window 对象 浏览器会在其打开一个 HTML 文档时创建一个对应的 window 对象.但是,如果一个文档定义了一个或多个框架(即,包含一个或多个 frame 或 iframe 标签),浏览器就 ...

  10. Android EventBus3.x 使用详解

    ♪(^∇^*) 五一假期在家无事,新项目中用的是RxJava2+EventBus感觉还不错,趁这闲暇总结下EventBus. 一.概要简述 EventBus是一个基于观察者模式的Android事件发布 ...