Skip to content

LeetCode 59. 螺旋矩阵 II

作者:Choi Yang
更新于:6 个月前
字数统计:639 字
阅读时长:3 分钟
阅读量:

题目描述

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

示例:

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

来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/spiral-matrix-ii 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路

按照螺旋矩阵模拟即可,先从左到右,在从上到下,再从右到左,再从下到上。

每次进行cur++操作,直到累加到total为止。最后返回二维数组即可(没想到 js二维数组也是这样方便...)

javascript
/**
 * @param {number} n
 * @return {number[][]}
 */
var generateMatrix = function (n) {
  let top = 0,
    bottom = n - 1;
  let left = 0,
    right = n - 1;
  let res = [];
  for (let i = 0; i < n; i++) res[i] = [];
  let cur = 1,
    total = n * n;
  while (cur <= total) {
    for (let i = left; i <= right; i++) res[top][i] = cur++; // 从左到右
    top++;
    for (let i = top; i <= bottom; i++) res[i][right] = cur++; // 从上到下
    right--;
    for (let i = right; i >= left; i--) res[bottom][i] = cur++; // 从右到左
    bottom--;
    for (let i = bottom; i >= top; i--) res[i][left] = cur++; // 从下到上
    left++;
  }
  return res;
};
cpp
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> res(n, vector<int>(n, 0));
        int top = 0, bottom = n - 1;
        int left = 0, right = n - 1;
        int cur = 1, total = n * n;
        while (cur <= total) {
            for (int i = left; i <= right; i++) res[top][i] = cur++; // 从左到右
            top++;
            for (int i = top; i <= bottom; i++) res[i][right] = cur++; // 从上到下
            right--;
            for (int i = right; i >= left; i--) res[bottom][i] = cur++; // 从右到左
            bottom--;
            for (int i = bottom; i >= top; i--) res[i][left] = cur++; // 从下到上
            left++;
        }
        return res;
    }
};
java
class Solution {
    public int[][] generateMatrix(int n) {
        int[][] res = new int[n][n];
        int top = 0, bottom = n - 1;
        int left = 0, right = n - 1;
        int cur = 1, total = n * n;
        while (cur <= total) {
            for (int i = left; i <= right; i++) res[top][i] = cur++; // 从左到右
            top++;
            for (int i = top; i <= bottom; i++) res[i][right] = cur++; // 从上到下
            right--;
            for (int i = right; i >= left; i--) res[bottom][i] = cur++; // 从右到左
            bottom--;
            for (int i = bottom; i >= top; i--) res[i][left] = cur++; // 从下到上
            left++;
        }
        return res;
    }
}
python
class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        res = [[0] * n for _ in range(n)]
        top, bottom = 0, n - 1
        left, right = 0, n - 1
        cur, total = 1, n * n
        while cur <= total:
            for i in range(left, right + 1): res[top][i] = cur; cur += 1 # 从左到右
            top += 1
            for i in range(top, bottom + 1): res[i][right] = cur; cur += 1 # 从上到下
            right -= 1
            for i in range(right, left - 1, -1): res[bottom][i] = cur; cur += 1 # 从右到左
            bottom -= 1
            for i in range(bottom, top - 1, -1): res[i][left] = cur; cur += 1 # 从下到上
            left += 1
        return res
javascript
学如逆水行舟,不进则退

Contributors

Choi Yang