daily leetcode - spiral-matrix-ii - !
题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 题目描述 Given a positive integer n , generate a square matrix filled with elements from 1 to n 2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 思路 此题跟之前那道 Spiral Matrix 本质上没什么区别,就相当于个类似逆运算的过程,这道题是要按螺旋的顺序来填数,由于给定矩形是个正方形,我们计算环数时用 n / 2 来计算,若 n 为奇数时,此时最中间的那个点没有被算在环数里,所以最后需要单独赋值,还是下标转换问题是难点,参考之前 Spiral Matrix 的讲解来转换下标吧。 关键点解析 代码 解法一: class Solution { public: vector<vector<int>> generateMatrix(int n) ....