1.题目描述
Given a positive integer n , generate a square matrix filled with elements from 1 to n 2 in spiral order.
给定一个正整数 n ,生成一个包含 1 到 n 2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。
Example:
Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
2.Solutions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
public static int [][] generateMatrix(int n) { int [][] ret = new int [n][n]; int left = 0 ,top = 0 ; int right = n -1 ,down = n - 1 ; int count = 1 ; while (left <= right) { for (int j = left; j <= right; j++) { ret[top][j] = count++; } top++; for (int i = top; i <= down; i++) { ret[i][right] = count++; } right--; for (int j = right; j >= left; j--) { ret[down][j] = count++; } down--; for (int i = down; i >= top; i--) { ret[i][left] = count++; } left++; } return ret; }
参考:【LeetCode】54. Spiral Matrix
(完)
近期评论