螺旋矩阵,简单题,有几个问题

class Solution {
private:
static constexpr int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public:
vector spiralOrder(vector<vector>& matrix) {
if (matrix.size() == 0 || matrix[0].size() == 0) {
return {};
}

    int rows = matrix.size(), columns = matrix[0].size();
    vector<vector<bool>> visited(rows, vector<bool>(columns));
    int total = rows * columns;
    vector<int> order(total);

    int row = 0, column = 0;
    int directionIndex = 0;
    for (int i = 0; i < total; i++) {
        order[i] = matrix[row][column];
        visited[row][column] = true;
        int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
        if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) {
            directionIndex = (directionIndex + 1) % 4;
        }
        row += directions[directionIndex][0];
        column += directions[directionIndex][1];
    }
    return order;
}

};
这是力扣第54道原题,螺旋矩阵的官方答案的方法一模拟法,我有几个问题,来个认真负责的大佬
1.directionIndex = (directionIndex + 1) % 4;这个是什么意思,有什么作用,%4我很困惑,希望讲的详细一点
2.static constexpr int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};为什么要用静态static constexpr int呢,我设置一个vector<vector<>>不好吗
3.row += directions[directionIndex][0];column += directions[directionIndex][1];我看这个我蒙了,我知道这是代表位移,但是他是怎么判断出你应该往左还是往右的往上还是往下的,我知道前面有个if,但是nextrow在if之前啊,希望讲的详细一点

1.
先看int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
这句话你应该知道吧,也就是下一个点该是什么,directionIndex=0的时候这句话就是就是x方向+1,y方向不动,directionIndex=1的时候这句话就是y+1,x不动,directionIndex等于2和3同理,所以directions就是方向,该向哪个方向移动,由directionIndex的值来控制。所以directionIndex的值是不能超过3,%4就是保证他的取值为0,1,2,3.
directionIndex = (directionIndex + 1) % 4;这个是用来判断螺旋矩阵前景方向的,当前方向上,下一个位置是出边界了还是已经遍历完毕了,如果遍历或者越界,则需要拐弯,而从题意和directions设计来看,旋转方向是右-》下-》左-》上-》右这种顺序执行的,所以+1代表下一个方向是哪个方向,%4求余是为了保证directionIndex取值不超过3.举例来说就是假设当前方向是向上,directionIndex=3,走到底之后下一个方向是向右,(directionIndex + 1) % 4=0,directionIndex =0的时候directions[0]=[0,1],代入最上面的那行代码中你就会发现y值不变,x值加1,那么他的方向就是向右移动。

2.
constexpr关键字的作用和const的区别,你用vector当然可以啊,但是vector可能会被修改的,不修改就一点问题都没有
https://www.zhihu.com/question/35614219/answer/798370856
3.
至于第三点,你看懂了我上面说的directionIndex 和directions的作用你就知道了

第一个问题,取模4 是为了保证directionIndex取值永远小于4,并且在这个周期内转。
第二个问题,你要知道使用 constexpr 修饰的变量,在编译期而非运行期就能确保其由常量表达式初始化为常量。vector也可以使用,只是向量是一个能够存放任意类型的动态数组。只是这里题主存放的都是整型,用constexpr更简单一些。
第三个问题,循环拿到下一行和下一列的位置,你的方向是按照右下左上右这个方向旋转的,你可以加行打印输出就一清二楚了

1.directionIndex = (directionIndex + 1) % 4代表 0,1,2,3四个方向;
directionIndex取0,directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};;则用到{0,1},directions[0][1]取1 nextColumn 自增,向右
directionIndex取1,directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};;则用到{1, 0},directions[1][0]取1 nextRow 自增,向下
directionIndex取2,directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};则用到{0, -1},这时 directions[2][1]取-1,nextColumn 自减,向左
directionIndex取3,directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};则用到{-1, 0},这时 directions[3][0]取-1,nextRow 自减,向上
回旋,从0继续,所以directionIndex需要取4的余数,其实就是方向判断
2.static constexpr int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};静态内部变量的优点;
3.row += directions[directionIndex][0];column += directions[directionIndex][1];这个其实就是
int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];保证下标合法性