https://oj.leetcode.com/problems/spiral-matrix-ii/
http://blog.csdn.net/linhuanmars/article/details/21906331
public class Solution { public int[][] generateMatrix(int n) { int[][] matrix = new int[n][n]; int num = 1; int len = n; int x = 0; while (n > 0) { if (n == 1) { matrix[x][x] = num; break; } // Circle // up for (int i = 0 ; i < n - 1 ; i ++) { matrix[x][x + i] = num; num ++; } // right for (int i = 0 ; i < n - 1 ; i ++) { matrix[x + i][len - 1 - x] = num; num++; } // down for (int i = 0 ; i < n - 1 ; i ++) { matrix[len - 1 - x][len - 1 - x - i] = num; num ++; } // left for (int i = 0 ; i < n - 1 ; i ++) { matrix[len - 1 - x - i][x] = num; num ++; } x ++; n -= 2; } return matrix; } }