Do not forget to assign size value of matrix. This is very important!!!!
1 class Solution { 2 public: 3 vector> generateMatrix(int n) { 4 vector > result(n, vector (n, 0)); 5 int count = 1; 6 for (int i = 0; i < n/2; i++) { 7 for (int j = i; j < n-i-1; j++) result[i][j] = count++; 8 for (int j = i; j < n-i-1; j++) result[j][n-i-1] = count++; 9 for (int j = n-i-1; j > i; j--) result[n-i-1][j] = count++;10 for (int j = n-i-1; j > i; j--) result[j][i] = count++;11 }12 if (n%2 == 1) result[n/2][n/2] = count;13 return result;14 }15 };