标签:螺旋矩阵
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) { vector<vector<int> > matrix; if(n < 0) return matrix; int **m = new int*[n]; for(int i = 0; i < n; i++) { m[i] = new int[n]; memset(m[i], 0, sizeof(int)*n); } int currentnum = 1; int loop = 0; while(loop < (n+1)/2) { int tmp = n - 1 - loop; for(int i = loop; i <= tmp; i++) m[loop][i] = currentnum++; for(int i = loop + 1; i <= tmp; i++) m[i][tmp] = currentnum++; for(int i = tmp - 1; i >= loop; i--) m[tmp][i] = currentnum++; for(int i = tmp - 1; i >= loop + 1; i--) m[i][loop] = currentnum++; loop++; } for(int i = 0; i < n; i++) { vector<int> tmp; for(int j = 0; j < n; j++) tmp.push_back(m[i][j]); matrix.push_back(tmp); } for(int i = 0; i < n; i++) delete[] m[i]; delete[] m; return matrix; } };
每日算法之四十一:Spiral Matrix II (螺旋矩阵),布布扣,bubuko.com
每日算法之四十一:Spiral Matrix II (螺旋矩阵)
标签:螺旋矩阵
原文地址:http://blog.csdn.net/yapian8/article/details/38590229