Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3
,
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
class Solution { public: vector<vector<int> > generateMatrix(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<int> > result(n, vector<int>(n)); if (n == 0) return result; int num = 0; int layer = n/2; for (int i = 0; i < layer; ++i) { int j; for (j = i; j < n-1-i; ++j) result[i][j] = ++num; for (j = i; j < n-1-i; ++j) result[j][n-1-i] = ++num; for (j = n-1-i; j > i; --j) result[n-1-i][j] = ++num; for (j = n-1-i; j > i; --j) result[j][i] = ++num; } if (n%2 == 1) result[n/2][n/2] = ++num; return result; } };