Home > Back-end >  Leetcode 085 largest rectangular area submitted heap buffer overflow errors
Leetcode 085 largest rectangular area submitted heap buffer overflow errors

Time:06-06

Consult everybody, the following code submitted times: heap buffer overflow errors,
I use GCC 5.4.0 using the command [GCC - fsanitize=address - fno omit - frame - pointer - O1 - g 85. C - 85] o compiled after the run is not an error, please to see where the problem is a great god, looking for more than two hours, really can not find, thank you,
All the code is as follows,
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Static int CalArea (int * heights, int heightsSize)//20 ms 59.86%
{

If (heights==NULL | | heightsSize==0)
{
return 0;
}

int i=0;
int j=0;
Int maxArea=0;
Int res=0;

For (I=0; i {
If ((I + 1) & lt; HeightsSize & amp; & Heights [I] <=heights [I + 1])
{
continue;
}

Int minHeight=heights [I];

For (j=I; J & gt;=0; J -)
{
MinHeight=(minHeight & gt; Heights [j]? Heights [j] : minHeight);
MaxArea=minHeight * (I - j + 1);
Res=(res & gt; MaxArea? Res: maxArea);
}
}
return res;
}

Static int maximalRectangle (char * * matrix, int matrixSize, int * matrixColSize)
{

If (matrix==NULL | | matrixSize==0 & amp; & MatrixColSize==0)
{
return 0;
}

Int row_size=matrixSize;
Int column_size=* matrixColSize;
int i=0;
int j=0;
Int Ans=0;
Int Area=0;
Int allZero=0;

Int * HGT=(int *) malloc (sizeof (int) * column_size);
If (HGT==NULL)
{
return 0;
}

Memset (HGT, 0, column_size * sizeof (int));

For (I=0; i {
For (j=0; J & lt; Column_size; J++)
{
If (allZero==column_size)
{
Memset (HGT, 0, column_size * sizeof (int));
}

If (matrix [I] [j]=='1')
{
HGT [j] +=1;
AllZero=0;
}
The else
{
AllZero++;
}

//printf (" % c ", matrix [I] [j]);
}
//printf (" \ n ");
Area=CalArea (HGT, column_size);
Ans=(Ans & gt; Area? Ans: Area);
}

Free (HGT);
HGT=NULL;
Return Ans.

}


Int main (void)
{
Char * * PTR=NULL;
Int I, j;
Int row=4;
Int col=5;

PTR=(char * *) malloc (sizeof (char *) * * col (row));

For (I=0; i {
PTR [I]=(char *) malloc (col * sizeof (char));
For (j=0; J & lt; Col. J++)
{
If (rand () % 2)
{
PTR [I] [j]='1';
}
The else
{
PTR [I] [j]='0';
}

Printf (" % c ", PTR [I] [j]);
}
printf("\n");
}

printf("\n");

Int res=maximalRectangle (PTR, row, & amp; Col);

return 0;
}

CodePudding user response:

Debug method:
First determine where there is no wrong, and then gradually narrowed, find out the mistakes,

CodePudding user response:

I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10581430.html
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10768339.html
  • Related