Failed at a test case with size = 80 (C++)


#1

Anyone please help with the following code, I am not able to understand what’s wrong here.

vector<vector > Solution::generateMatrix(int A) {
vector<vector> mat(A, vector(A, 0));
int k = 1;
int t = 0, l = 0, r = A-1, b = A-1, d = 0;
while(t<b && l<r)
{
if(d==0)
{
for(int i = l; i <= r; i++)
{
mat[t][i] = k;
k++;
}
d = 1; t++;
}
else if(d==1)
{
for(int i = t; i <= b; i++)
{
mat[i][r] = k;
k++;
}
d = 2; r–;
}
else if(d==2)
{
for(int i = r; i >= l; i–)
{
mat[b][i] = k;
k++;
}
d = 3; b–;
}
else if(d==3)
{
for(int i = b; i >= t; i–)
{
mat[i][l] = k;
k++;
}
d = 0; l++;
}
}
return mat;
}


#2

while(t<=b && l<=r) this was the change


#3

Did you got any solution