15. 3Sum (重新分配数组大小)
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 (重新分配数组大小)的更多相关文章
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- C++中delete[]是如何知道数组大小的
先看一段代码: int main(void) { int *pI = new int; int *pArray = new int[10]; int size = *(pArray-1); delet ...
- c++: 获取delete[]中的数组大小
看一个小例子: 1 #include <iostream> 2 3 using namespace std; 4 5 class A { 6 public: 7 A() { ...
- 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 ...
- 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 ...
- 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 = ...
- C++ 数组作为函数参数时,传递数组大小的方法
废话不多说,先上错误示范: void fun(int arr[arr_num]) { // ... } int main() { // ... int *arr = new int[10]; fun( ...
- ylbtech-Java-Runoob-高级教程-实例-数组:15. Java 实例 – 判断数组是否相等
ylbtech-Java-Runoob-高级教程-实例-数组:15. Java 实例 – 判断数组是否相等 1.返回顶部 1. Java 实例 - 判断数组是否相等 Java 实例 以下实例演示了如 ...
- 将一组数组向右移动k位,末尾的要转置移动到数组开始,其中n为数组大小,0<k<n
下面是使用a数组本身完成: package 数组元素k位右移; /** * 数组向又移动k位. 0<k<n * * @author SeeClanUkyo 将一组数组向右移动k位,末尾的要 ...
随机推荐
- AspxGridView使用手记
AspxGridView使用手记 一. 基本使用方法 4 1.导入Dll文件 4 2.Asp.Net页面控件注册 4 3. Asp.Net页面控件声明 5 4.删除licenses. ...
- java的原子类 AtomicInteger 实现原理是什么?
采用硬件提供原子操作指令实现的,即CAS.每次调用都会先判断预期的值是否符合,才进行写操作,保证数据安全. CAS机制 CAS是英文单词Compare And Swap的缩写,翻译过来就是比较并替换. ...
- IO 和 NIO 的区别
IO:阻塞IO,面向流:当一个线程调用read() 或 write()时,该线程被阻塞,直到有一些数据被读取,或数据完全写入,该线程在此期间不能再干任何事情了:可以有少量的连接使用非常高的带宽,一次发 ...
- Xshell 公钥登入服务器
1:生成公钥 此时有test.pub文件 2:linux CentOS 7 配置 2.1 test.pub 存入/root/test.pub目录下面 2.2 确保authorized_keys文件内容 ...
- package-info.java
参考文章: http://blog.sina.com.cn/s/blog_93dc666c0101gzlr.html 对于package-info.java我们并不陌生,但又陌生. 在我们每次建立pa ...
- APP-5-百度电子围栏
1.代码部分 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...
- pycharm 直接删掉数据表之后,makemigration和migrate 之后,数据库中依然没有生成数据表的问题
综合分析一下行程这个问题的原因: 在终端中运行 select * from django_migrations; 查看 提交的记录,如果你的表删掉了,记录还在,那么数据库会觉得,这个表依然是存在的,所 ...
- FlashDevelop关闭分号自动格式化
菜单Tools-Program Settings-ASCompletion-Helpers-Characters Requiring Whitespace 清空",;*+-=/%<&g ...
- csredis base usage
Basic usage Whenever possible, server responses are mapped to the appropriate CLR type. using (var r ...
- RxJava2.0学习笔记2 2018年7月3日 周二
摘记: 1.map -- 转换 有些服务端的接口设计,会在返回的数据外层包裹一些额外信息,这些信息对于调试很有用,但本地显示是用不到的.使用 map() 可以把外层的格式剥掉,只留下本地会用到的核心 ...