AC Leetcode 59. 螺旋矩阵 II

简介: AC Leetcode 59. 螺旋矩阵 II

[59. 螺旋矩阵 II
](https://leetcode.cn/problems/spiral-matrix-ii/)

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

示例 1:
在这里插入图片描述
输入:n = 3
输出:[[1,2,3],[8,9,4],[7,6,5]]
示例 2:

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

提示:
1 <= n <= 20

解题思路

1.对于每层,从左上方开始以顺时针的顺序填入所有元素。假设当前层的左上角位于 (\textit{top}, \textit{left})(top,left),右下角位于 (\textit{bottom}, \textit{right})(bottom,right),按照如下顺序填入当前层的元素。
2.从左到右填入上侧元素,依次为 (\textit{top}, \textit{left})(top,left) 到 (\textit{top}, \textit{right})(top,right)。
3.从上到下填入右侧元素,依次为 (\textit{top} + 1, \textit{right})(top+1,right) 到 (\textit{bottom}, \textit{right})(bottom,right)。
4.如果 \textit{left} < \textit{right}left<right 且 \textit{top} < \textit{bottom}top<bottom,则从右到左填入下侧元素,依次为 (\textit{bottom}, \textit{right} - 1)(bottom,right−1) 到 (\textit{bottom}, \textit{left} + 1)(bottom,left+1),以及从下到上填入左侧元素,依次为 (\textit{bottom}, \textit{left})(bottom,left) 到 (\textit{top} + 1, \textit{left})(top+1,left)。
5.填完当前层的元素之后,将 \textit{left}left 和 \textit{top}top 分别增加 11,将 \textit{right}right 和 \textit{bottom}bottom 分别减少 11,进入下一层继续填入元素,直到填完所有元素为止。
在这里插入图片描述

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];
        int left = 0, right = n - 1, top = 0, bottom = n - 1;
        int i = 0, j = 0;
        int cur = 0;
        int max = (int) Math.pow(n, 2);

        while (cur <= max) {
            while (j <= right) {
                result[i][j++] = ++cur;
            }
            top++;
            j--;
            i = top;

            if (cur == max) {
                break;
            }
            while (i <= bottom) {
                result[i++][j] = ++cur;
            }
            i--;
            right--;
            j = right;
            if (cur == max) {
                break;
            }
            while (j >= left) {
                result[i][j--] = ++cur;
            }
            j++;
            bottom--;
            i = bottom;
            if (cur == max) {
                break;
            }
            while (i >= top) {
                result[i--][j] = ++cur;
            }
            i++;
            left++;
            j = left;
        }
        return result;
    }
}
目录
相关文章
|
10月前
【Leetcode -2181.合并零之间的节点- 2326.螺旋矩阵Ⅳ】
【Leetcode -2181.合并零之间的节点- 2326.螺旋矩阵Ⅳ】
60 0
|
3天前
|
算法
LeetCode第59题螺旋矩阵 II
LeetCode第59题"螺旋矩阵 II"的解题方法,通过模拟螺旋填充过程,一圈一圈从外到内按顺序填充数字,直到完成整个矩阵的构建。
LeetCode第59题螺旋矩阵 II
|
3天前
|
存储 算法
LeetCode第54题螺旋矩阵
LeetCode第54题"螺旋矩阵"的解题方法,通过模拟从外到内的螺旋遍历过程,并利用方向向量控制遍历方向的转换,有效输出矩阵的螺旋顺序。
LeetCode第54题螺旋矩阵
|
2月前
leetcode54螺旋矩阵题解
leetcode54螺旋矩阵题解
21 2
|
2月前
|
算法 机器人 数据挖掘
LeetCode题目54:螺旋矩阵【python4种算法实现】
LeetCode题目54:螺旋矩阵【python4种算法实现】
|
2月前
|
存储 算法
力扣经典150题第三十五题:螺旋矩阵
力扣经典150题第三十五题:螺旋矩阵
10 0
|
10月前
|
机器学习/深度学习 算法
代码随想录Day02 数组基础2 leetcode T977有序数组的平方, T209 长度最小的子数组,T59 螺旋矩阵II
代码随想录Day02 数组基础2 leetcode T977有序数组的平方, T209 长度最小的子数组,T59 螺旋矩阵II
47 0
|
3月前
leetcode-6111:螺旋矩阵 IV
leetcode-6111:螺旋矩阵 IV
32 0
|
3月前
|
Java C++ Python
leetcode-59:螺旋矩阵 II
leetcode-59:螺旋矩阵 II
26 0
|
3月前
leetcode-54:螺旋矩阵
leetcode-54:螺旋矩阵
29 0