vector<vector<int>> generateMatrix(int n) { int num = 1; int top = 0, right = n - 1, bottom = n - 1, left = 0;
vector<vector<int>> ret(n,vector<int>(n)); while (num<=n*n) { //top for (int col = left; col <= right; col++) ret[top][col] = num++; if (++top > bottom) break;
//right for (int row = top; row <= bottom; row++) ret[row][right] = num++; if (--right < left) break;
//bottom for (int col = right; col >= left; col--) ret[bottom][col] = num++; if (--bottom < top) break;
近期评论