# Memory Limit Exceed

• Here is my code, and it occurs a Memory Limit Exceed error...
It runs well on local.
can anybody help me?
Thanks!

bool run(vector<vector<int> > &matrix, vector<int> &result, int m, int n)
{

``````for(int i=1; i<m; i++)
{
result.push_back(matrix.at(0).at(i));
}
for(int j=0; j<n; j++)
{
result.push_back(matrix.at(j).at(m-1));
}
if(n!=1)
{
for(int k=m-2; k>=0; k--)
{
result.push_back(matrix.at(n-1).at(k));
}
}
if(m!=1)
{
for(int l=n-2; l>=1; l--)
{
result.push_back(matrix.at(l).at(0));
}
}

if(n-2>=1)
{
return true;
}
else
{
return false;
}
``````

}

vector<int> spiralOrder(vector<vector<int> > &matrix)
{

``````vector<int> result;
result.clear();
if(matrix.size()==0)
{
return result;
}
int m = matrix.at(0).size();
int n = matrix.size();

bool flag = run(matrix, result, m, n);
while(flag)
{
if(m-2>0 && n-2>0)
{
for(int i=0; i<n; i++)
{
vector<int>::iterator itr = matrix.at(i).begin();
matrix.at(i).erase(itr);
matrix.at(i).pop_back();
}
vector<vector<int> >::iterator it = matrix.begin();
matrix.erase(it);
matrix.pop_back();
m = m-2;
n = n-2;
}
flag = run(matrix, result, m, n);
}
return result;
``````

}

• I've got the same problem. Do not want can be improved. I have used only necessary spaces. How to avoid "memory limit exceeded"?

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.