当前位置:网站首页>Leecode-c language implementation -15 Sum of three ----- ideas to be improved
Leecode-c language implementation -15 Sum of three ----- ideas to be improved
2022-07-06 07:25:00 【Sunshine clover lxgzxj】
One 、 subject

Two 、 Their thinking
(1) Ideas need to be improved
For example, the array is :{
-1,0,1,0,3}
(1) Construct a -10^5 To 10^5 Array of , Put the data one by one , Here for the convenience of writing -5 To 5.
(2) Define multiple variables to record the maximum and minimum subscripts and indexes when placing .
The number :-5 -4 -3 -2 -1 0 1 2 3 4 5
frequency : 0 0 0 0 1 1 1 0 1 0 0
Indexes : 0 1 2 3 4 5 6 7 8 9 10
min max
temp
The length of the array :11
MinIndex = 4
MaxIndex = 6
TempIndex = 4
MaxValue = 3
MinValue = -1
(3) If the minimum value is greater than zero , It means that no addition in the array can be 0, Returns a null value .
(4) If the maximum value is less than zero , It means that no addition in the array can be 0, Returns a null value .
(5) If twice the minimum plus the maximum is still greater than 0, It shows that this maximum value is definitely not in the final answer .
-1 * 2 + 3 > 0
MaxIndex Minus one
MaxIndex = 6
(5) If twice the maximum plus the minimum is still less than 0, It shows that this minimum value is definitely not in the final answer .
(6) Judgment logic :MaxValue + MinValue + TempValue = 0, recorded TempIndex by 5 .
-1 + 1 + 0 = 0
(7) To determine TempIndex=5 The corresponding value is 1, Explain that there is only one in the array 0, And it does not coincide with the maximum and minimum values , Meet the requirements .
MaxIndex Minus one
MaxIndex = 6
Finally, add an array to the two-dimensional array :{
{
-1 0 1 } }
(8) To determine :
MinValue : -1 , MaxValue : 0 , TempValue : 1
Find out TempValue : 1 exceed MaxValue : 0, For repeating elements , Kicked out .
(9) To determine :
MinValue : 0 , MaxValue : 1 , TempValue : -1
Find out TempValue : 1 exceed MinValue : 0, For repeating elements , Kicked out .
(10) To determine :
MinValue : 0 , MaxValue : 0 , TempValue : 0
Find out 0 There are only two values , Here I took it three times , Kicked out .
3、 ... and 、VS Test code
(1) Ideas need to be improved
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ELEMENTSIZE 3
#define INITARRAYSIZE 10000
#define RECORDARRAYSIZE 10
void PrintfArr(void *arr, int size, int elementsize);
void PrintfHeadVal();
void Printf2DimensionArray(int** Array, int num1, int num2);
int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes);
int main()
{
int nums[] = {
-1,0,1,0,3 };
int numsSize = sizeof(nums) / sizeof(int);
int returnSize = 0;
int** returnColumnSizes = (int**)malloc(sizeof(int*));
PrintfArr(nums, numsSize, sizeof(int));
threeSum(nums, numsSize, &returnSize, returnColumnSizes);
return 0;
}
/* For example, the array is : The number :-5 -4 -3 -2 -1 0 1 2 3 4 5 frequency : 0 1 2 1 3 1 0 2 4 5 1 Indexes : 0 1 2 3 4 5 6 7 0 9 10 min max temp The length of the array :11 (1) Construct a -10^5 To 10^5 Array of , Put the data one by one . (2) Define two variables to record the maximum and minimum subscripts when inserting . (3) If the minimum value is greater than zero , It means that no addition in the array can be 0, Returns a null value . (4) If the maximum value is less than zero , It means that no addition in the array can be 0, Returns a null value . (5) That is, there are three variables ,MaxIndex,MinIndex,TempIndex. (6) Judgment logic :max Value + min Value + temp Value = 0, recorded . max Value + min Value + temp Value != 0, Don't record . (7)min <= 0, max >= 0, temp Value = 0 - min Value - max Value , To judge temp The index of the bit . (8)temp Whether the corresponding index bit is 0, If by 0, Don't record . by 1 You need to judge whether it is with max、min coincidence , Coincident exit , Non coincident records . Greater than, etc 2, Record . */
int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
*returnSize = 0;
if (numsSize < ELEMENTSIZE)
{
return NULL;
}
int** ResArray = (int**)malloc(sizeof(int*) * INITARRAYSIZE);
int* RecordArray = (int*)malloc(sizeof(int) * (RECORDARRAYSIZE * 2 + 1));
*returnColumnSizes = (int*)malloc(sizeof(int) * INITARRAYSIZE);
int MaxIndex = RECORDARRAYSIZE + nums[0];
int MinIndex = RECORDARRAYSIZE + nums[0];
int TempValue = 0;
int MaxValue = MaxIndex - RECORDARRAYSIZE;
int MinValue = MinIndex - RECORDARRAYSIZE;
int TempIndex = 0;
int i = 0;
int j = 0;
int Flag = 0;
// Initialize array RecordArray
for ( i = 0; i < RECORDARRAYSIZE * 2 + 1; i++)
{
RecordArray[i] = 0;
}
// take nums The values in the array are recorded in the array RecordArray in
// Record the maximum and minimum values and index
for ( i = 0; i < numsSize; i++)
{
MaxValue = MaxIndex - RECORDARRAYSIZE;
if (nums[i] > MaxValue)
{
MaxIndex = RECORDARRAYSIZE + nums[i];
}
MinValue = MinIndex - RECORDARRAYSIZE;
if(nums[i] < MinValue)
{
MinIndex = RECORDARRAYSIZE + nums[i];
}
RecordArray[RECORDARRAYSIZE + nums[i]]++;
}
MaxValue = MaxIndex - RECORDARRAYSIZE;
MinValue = MinIndex - RECORDARRAYSIZE;
PrintfHeadVal();
printf(" The number :");
PrintfArr(RecordArray, RECORDARRAYSIZE * 2 + 1, sizeof(int));
printf("MaxIndex : %d , MaxValue : %d , MinIndex : %d , MinValue : %d\n", MaxIndex, MaxValue, MinIndex, MinValue);
// If the minimum value is greater than zero , It means that no addition in the array can be 0, Returns a null value .
// If the maximum value is less than zero , It means that no addition in the array can be 0, Returns a null value .
if (MaxValue < 0)
{
return NULL;
}
if (MinValue > 0)
{
return NULL;
}
// The following two cycles can reduce the following for Number of cycles , Speed up program efficiency .
// It's OK to , The efficiency is not improved .
while ((MinValue * 2 + MaxValue > 0) && (RecordArray[MaxIndex] != 0))
{
MaxIndex--;
MaxValue = MaxIndex - RECORDARRAYSIZE;
}
while ((MinValue + MaxValue * 2 < 0) && (RecordArray[MinIndex] != 0))
{
MinIndex++;
MinValue = MinIndex - RECORDARRAYSIZE;
}
for (i = MinIndex; i <= RECORDARRAYSIZE; i++)
{
if (RecordArray[i] == 0)
{
continue;
}
MinValue = i - RECORDARRAYSIZE;
while ((MinValue * 2 + MaxValue > 0) && (RecordArray[MaxIndex] != 0))
{
MaxIndex--;
MaxValue = MaxIndex - RECORDARRAYSIZE;
}
for ( j = MaxIndex; j >= RECORDARRAYSIZE; j--)
{
if (RecordArray[j] == 0)
{
continue;
}
MaxValue = j - RECORDARRAYSIZE;
TempValue = 0 - MinValue - MaxValue;
TempIndex = TempValue + RECORDARRAYSIZE;
printf(" i : %2d , MinValue : %2d ,j : %2d , MaxValue : %2d , TempIndex : %2d , TempValue : %2d , RecordArray[TempIndex] : %2d\n",
i, MinValue, j, MaxValue, TempIndex, TempValue, RecordArray[TempIndex]);
if (RecordArray[TempIndex] > 0)
{
if ((RecordArray[TempIndex] == 1) && (j > TempIndex) && (i < TempIndex))
{
Flag = 1;
}
else if ((RecordArray[TempIndex] == 2) && (j > TempIndex) && (i <= TempIndex))
{
Flag = 1;
}
else if ((RecordArray[TempIndex] == 2) && (j >= TempIndex) && (i < TempIndex))
{
Flag = 1;
}
else if ((RecordArray[TempIndex] > 2) && (j >= TempIndex) && (i <= TempIndex))
{
Flag = 1;
}
if (Flag)
{
printf("*returnSize : %d\n", *returnSize);
(*returnColumnSizes)[*returnSize] = ELEMENTSIZE;//*returnColumnSizes Put it in parentheses
ResArray[*returnSize] = (int*)malloc(sizeof(int) * ELEMENTSIZE);
ResArray[*returnSize][0] = MinValue;
ResArray[*returnSize][2] = MaxValue;
ResArray[*returnSize][1] = TempValue;
//printf("data : (%d , %d , %d)\n", ResArray[*returnSize][0], ResArray[*returnSize][2], ResArray[*returnSize][1]);
(*returnSize)++;
Printf2DimensionArray(ResArray, *returnSize, ELEMENTSIZE);
printf("+++++++++++++++++++++++++++++\n");
Flag = 0;
}
}
}
}
Printf2DimensionArray(ResArray, *returnSize, ELEMENTSIZE);
Printf2DimensionArray(returnColumnSizes, 1, *returnSize);
return ResArray;
}
void Printf2DimensionArray(int** Array, int num1, int num2)
{
int i, j;
printf("{");
for ( i = 0; i < num1; i++)
{
printf("{");
for ( j = 0; j < num2; j++)
{
printf("%2d ", Array[i][j]);
}
printf("} ");
}
printf("} \n");
}
void PrintfHeadVal()
{
int i;
printf(" Header :");
for (i = -RECORDARRAYSIZE; i <= RECORDARRAYSIZE; i++)
{
printf("%3d ", i);
}
printf("\n");
printf(" Indexes :");
for (i = 0; i <= RECORDARRAYSIZE * 2 ; i++)
{
printf("%3d ", i);
}
printf("\n");
}
void PrintfArr(void *arr, int size, int elementsize)
{
if (elementsize == sizeof(int))
{
int *tmparr = (int*)arr;
int i;
for (i = 0; i < size; i++)
{
printf("%3d ", tmparr[i]);
}
}
else if (elementsize == sizeof(char))
{
char *tmparr = (char*)arr;
int i;
for (i = 0; i < size; i++)
{
printf("%c ", tmparr[i]);
}
}
printf("\n========================\n");
}
Four 、VS Running results

5、 ... and 、leecode Submission code
#define ELEMENTSIZE 3
#define INITARRAYSIZE 20000
#define RECORDARRAYSIZE 100000
int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
*returnSize = 0;
if (numsSize < ELEMENTSIZE)
{
return NULL;
}
int** ResArray = (int**)malloc(sizeof(int*) * INITARRAYSIZE);
int* RecordArray = (int*)malloc(sizeof(int) * (RECORDARRAYSIZE * 2 + 1));
*returnColumnSizes = (int*)malloc(sizeof(int) * INITARRAYSIZE);
int MaxIndex = RECORDARRAYSIZE + nums[0];
int MinIndex = RECORDARRAYSIZE + nums[0];
int TempValue = 0;
int MaxValue = MaxIndex - RECORDARRAYSIZE;
int MinValue = MinIndex - RECORDARRAYSIZE;
int TempIndex = 0;
int i = 0;
int j = 0;
int Flag = 0;
for ( i = 0; i < RECORDARRAYSIZE * 2 + 1; i++)
{
RecordArray[i] = 0;
}
for ( i = 0; i < numsSize; i++)
{
MaxValue = MaxIndex - RECORDARRAYSIZE;
if (nums[i] > MaxValue)
{
MaxIndex = RECORDARRAYSIZE + nums[i];
}
MinValue = MinIndex - RECORDARRAYSIZE;
if(nums[i] < MinValue)
{
MinIndex = RECORDARRAYSIZE + nums[i];
}
RecordArray[RECORDARRAYSIZE + nums[i]]++;
}
MaxValue = MaxIndex - RECORDARRAYSIZE;
MinValue = MinIndex - RECORDARRAYSIZE;
if (MaxValue < 0)
{
return NULL;
}
if (MinValue > 0)
{
return NULL;
}
while ((MinValue * 2 + MaxValue > 0) && (RecordArray[MaxIndex] != 0))
{
MaxIndex--;
MaxValue = MaxIndex - RECORDARRAYSIZE;
}
while ((MinValue + MaxValue * 2 < 0) && (RecordArray[MinIndex] != 0))
{
MinIndex++;
MinValue = MinIndex - RECORDARRAYSIZE;
}
for (i = MinIndex; i <= RECORDARRAYSIZE; i++)
{
if (RecordArray[i] == 0)
{
continue;
}
MinValue = i - RECORDARRAYSIZE;
while ((MinValue * 2 + MaxValue > 0) && (RecordArray[MaxIndex] != 0))
{
MaxIndex--;
MaxValue = MaxIndex - RECORDARRAYSIZE;
}
for ( j = MaxIndex; j >= RECORDARRAYSIZE; j--)
{
if (RecordArray[j] == 0)
{
continue;
}
MaxValue = j - RECORDARRAYSIZE;
TempValue = 0 - MinValue - MaxValue;
TempIndex = TempValue + RECORDARRAYSIZE;
if (RecordArray[TempIndex] > 0)
{
if ((RecordArray[TempIndex] == 1) && (j > TempIndex) && (i < TempIndex))
{
Flag = 1;
}
else if ((RecordArray[TempIndex] == 2) && (j > TempIndex) && (i <= TempIndex))
{
Flag = 1;
}
else if ((RecordArray[TempIndex] == 2) && (j >= TempIndex) && (i < TempIndex))
{
Flag = 1;
}
else if ((RecordArray[TempIndex] > 2) && (j >= TempIndex) && (i <= TempIndex))
{
Flag = 1;
}
if (Flag)
{
(*returnColumnSizes)[*returnSize] = ELEMENTSIZE;
ResArray[*returnSize] = (int*)malloc(sizeof(int) * ELEMENTSIZE);
ResArray[*returnSize][0] = MinValue;
ResArray[*returnSize][2] = MaxValue;
ResArray[*returnSize][1] = TempValue;
(*returnSize)++;
Flag = 0;
}
}
}
}
return ResArray;
}
6、 ... and 、 Running results

边栏推荐
- 【mysql学习笔记29】触发器
- You deserve this high-value open-source third-party Netease cloud music player
- Crawling exercise: Notice of crawling Henan Agricultural University
- Methods for JS object to obtain attributes (. And [] methods)
- Excel的相关操作
- Sélectionnez toutes les lignes avec un symbole dans Word et changez - les en titre
- 杰理之BLE【篇】
- 杰理之普通透传测试---做数传搭配 APP 通信【篇】
- 杰理之BLE【篇】
- 杰理之开发板上电开机,就可以手机打开 NRF 的 APP【篇】
猜你喜欢

jmeter性能测试步骤实战教程

OpenGL ES 学习初识(1)

Crawling exercise: Notice of crawling Henan Agricultural University

Set picture annotation in markdown

软件测试界的三无简历,企业拿什么来招聘你,石沉大海的简历
![[window] when the Microsoft Store is deleted locally, how to reinstall it in three steps](/img/57/ee979a7db983ad56f0df7345dbc91f.jpg)
[window] when the Microsoft Store is deleted locally, how to reinstall it in three steps

Path analysis model

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

leetcode1020. Number of enclaves (medium)

Wechat official account infinite callback authorization system source code, launched in the whole network
随机推荐
Supervisor usage document
TypeScript 可索引类型
L'auteur est mort? Ai utilise l'art pour conquérir l'humanité
Word delete the contents in brackets
TS基础篇
#systemverilog# 可综合模型的结构总结
Is software testing outsourcing going or not? Three years' real outsourcing experience tells you
杰理之BLE【篇】
js对象获取属性的方法(.和[]方式)
If Jerry's Bluetooth device wants to send data to the mobile phone, the mobile phone needs to open the notify channel first [article]
How MySQL merges data
Do you really think binary search is easy
js對象獲取屬性的方法(.和[]方式)
Introduction to the basics of network security
Simple and understandable high-precision addition in C language
After the hot update of uniapp, "mismatched versions may cause application exceptions" causes and Solutions
When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
Go learning --- use reflection to judge whether the value is valid
First knowledge of OpenGL es learning (1)
SSM learning