【LeetCode】59.螺旋矩阵II

59.螺旋矩阵II

知识点:数组

题目描述

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

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

输入:n = 1
输出:[[1]]

解法一:

和54题解法基本一致,利用四个边界条件进行遍历;

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] ans = new int[n][n];
        int num = 1;
        int top = 0, down = n-1;
        int left = 0, right = n-1;
        while (true){
            for (int i = left; i <= right; i++){
                ans[top][i] = num;
                num++;
            }
            top++;
            if (top > down) break;
            for (int i = top; i <= down; i++){
                ans[i][right] = num;
                num++;
            }
            right--;
            if (left > right) break;
            for (int i = right; i >= left; i--){
                ans[down][i] = num;
                num++;
            }
            down--;
            if (top > down) break;
            for (int i = down; i >= top; i--){
                ans[i][left] = num;
                num++;
            }
            left++;
            if (left > right) break;
        }
        return ans;
    }
}
  • PYTHON
class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        matrix = [[0] * n for i in range(n)]    #注意二维矩阵的创建方法
        top, down = 0, n-1
        left, right = 0, n-1
        num = 1
        while True:
            for i in range(left, right+1):
                matrix[top][i] = num
                num += 1
            top += 1
            if top > down:
                break
            for i in range(top, down+1):
                matrix[i][right] = num
                num += 1
            right -= 1
            if left > right:
                break
            for i in range(right, left-1, -1):
                matrix[down][i] = num
                num += 1
            down -= 1
            if top > down:
                break
            for i in range(down, top-1, -1):
                matrix[i][left] = num
                num += 1
            left += 1
            if left > right:
                break
        return matrix

时间复杂度:O(N);

posted @ 2021-07-12 22:17  Curryxin  阅读(32)  评论(0编辑  收藏  举报
Live2D