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位,末尾的要 ...
随机推荐
- Mac安装Mysql-python _mysql.c:44:10: fatal error: 'my_config.h' file not found
解决步骤 brew install mysql brew unlink mysql brew install mysql-connector-c sed -i -e /bin/mysql_config ...
- day05-表的三种关系
表的三种关系 1)一对一 关联方式:foreign key+unique例如1个学生只能有1个学号,1个学号只能对应1个学生 #两张表: 学生表(student)和 学号表(student_id) # ...
- java 使用jsoup处理html字符
依赖的jar <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artif ...
- classloader trace
类加载机制: 程序启动时,根据入口函数调用相关功能,功能在不同类中即在不同的class文件中,jvm根据类加载机制来动态加载class文件到内存中,只有被加载后才能被调用,否则引发异常 1.装载:查找 ...
- 使用pt-table-checksum及pt-table-sync校验复制一致性
一.简介 pt-table-checksum是percona-toolkit系列工具中的一个, 可以用来检测主. 从数据库中数据的一致性.其原理是在主库上运行, 对同步的表进行checksum, 记录 ...
- linux /dev/null 中有数据
前段时间有个同事问我说,他 cat /dev/null有数据.这个颠覆大家认知的问题最终却是个小问题. 我们来看/dev/null的操作函数: static const struct memdev { ...
- Android高级控件(下)
Chronometer计时器 常用的方法 getBase() 基准时间 setFormat 设置显示格式 start() 开始计时 stop() 停止计时 setOnChronometerListen ...
- 启用Flash Player 11.3的全屏键盘输入注意事项
启用Flash Player 11.3的全屏键盘输入,注意以下事项: 1. HTML代码<param name=”allowFullScreenInteractive” value=”true” ...
- 二,Request和Response
概述 在DRF中,引入了一个Request和Response对象进行请求和响应,这两个对象分别继承于Djaong中常规的HttpRequest和SimpleTemplateResponse,相比其父类 ...
- tensflow分布式
https://blog.csdn.net/CodeMaster_/article/details/76223835 代码解析好文: https://wenku.baidu.com/view/94b2 ...