[LeetCode] 59. 螺旋矩阵 II

题目链接: https://leetcode-cn.com/problems/spiral-matrix-ii/

题目描述:

给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。

示例:

输入: 3
输出:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]

思路:

与上一题 54. 螺旋矩阵一样,题解链接

还是模拟过程,控制好

行的上下边界, 列的左右边界!即可

时间复杂度: \(O(n^2)\)

代码:

python

class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        above_row = 0
        below_row = n - 1
        left_col = 0
        right_col = n - 1
        res = [ [0] * n for _ in range(n) ]
        num = 1
        while above_row <= below_row and left_col <= right_col:
            # 从左到右
            for i in range(left_col, right_col+1):
                res[above_row][i] = num
                num += 1
            # 上行加1
            above_row += 1
            # 从上到下
            for i in range(above_row, below_row+1):
                res[i][right_col] = num
                num += 1
            right_col -= 1
            # 从右到左
            for i in range(right_col, left_col-1, -1):
                res[below_row][i] = num
                num += 1
            below_row -= 1
            #从下到上
            for i in range(below_row, above_row-1, -1):
                res[i][left_col] = num
                num += 1
            left_col += 1
        return res

java

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] res = new int[n][n];
        int above_row = 0;
        int below_row = n - 1;
        int left_col = 0;
        int right_col = n - 1;
        int num = 1;
        while (above_row <= below_row && left_col <= right_col) {
            // 从左到右
            for (int i = left_col; i <= right_col; i++) {
                res[above_row][i] = num;
                num++;
            }
            above_row++;
            // 从上到下
            for (int i = above_row; i <= below_row; i++) {
                res[i][right_col] = num;
                num++;
            }
            right_col--;
            // 从右到左
            for (int i = right_col; i >= left_col; i--) {
                res[below_row][i] = num;
                num++;
            }
            below_row--;
            // 从下到上
            for (int i = below_row; i >= above_row; i--) {
                res[i][left_col] = num;
                num++;
            }
            left_col++;
        }
        return res;  
    }
}

posted on 2019-05-22 17:44  威行天下  阅读(142)  评论(0编辑  收藏  举报

导航