885. 螺旋矩阵 III

2022年 8月 21日 71点热度 0人点赞

file

我写的

下面是我写的螺旋行走


#include <iostream>               // 输入输出
#include <vector>             // 可变长度数组
#include <unordered_map>      // hashmap
#include <stack>              // 栈
#include <string>             // 字符串

using namespace std;

/// <summary>
/// 输出容器中的内容
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
template<typename T>
void print(T t) {

    for (typename T::const_iterator it = t.begin(); it != t.end() - 1; ++it) {
        cout << *it << ", ";
    }

    cout << *(t.end() - 1) << endl;
}

////////////////////////////////////////////////////////////////////////////////
/// 这里放 OJ 的类

class Solution {
public:

    inline bool inBox(int x, int y, int row, int col) {

        bool res = 0 <= x && x <= col - 1 && 0 <= y && y <= row - 1;

        /*
        std::cout << "x = " << x << ", y = " << y << ", row = " << row << ", col = " << col;

        if (res) {
            cout << "    IN  " << endl;
        }
        else {
            cout << endl;
        }

        */
        return res;
    }

    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
        int max_row_layer = (rStart - 0) > (rows - rStart) ? (rStart - 0) : (rows - rStart);
        int max_col_layer = (cStart - 0) > (cols - cStart) ? (cStart - 0) : (cols - cStart);
        int max_layer = max_row_layer > max_col_layer ? max_row_layer : max_col_layer;

        vector<vector<int>> lay_point;

        lay_point.push_back({ rStart, cStart });

        int x = 0;
        int y = 0;

        // 循环多少层
        for (int i = 1; i <= max_layer; i++) {

            cout << "======================================" << endl;

            // 计算出第一个点
            x = cStart + i;
            y = rStart + 1 - i;

            int last_x = 0, last_y = 0;

            // 右边
            for (int j = 0; j < 2 * i; ++j) {
                if (inBox(x, y + j, rows, cols)) {
                    lay_point.push_back({ y + j, x });
                }
                // 记录当前的最后一个点
                if (j == 2 * i - 1) {
                    last_x = x - 1;
                    last_y = y + j;
                }
            }

            x = last_x;
            y = last_y;

            // 下边
            for (int j = 0; j < 2 * i; ++j) {
                if (inBox(x - j, y, rows, cols)) {
                    lay_point.push_back({ y, x - j });
                }
                if (j == 2 * i - 1) {
                    last_x = x - j;
                    last_y = y - 1;
                }
            }

            x = last_x;
            y = last_y;

            // 左边

            for (int j = 0; j < 2 * i; ++j) {
                if (inBox(x, y - j, rows, cols)) {
                    lay_point.push_back({ y - j, x, });
                }
                if (j == 2 * i - 1) {
                    last_x = x + 1;
                    last_y = y - j;
                }
            }

            x = last_x;
            y = last_y;

            // 上边
            for (int j = 0; j < 2 * i; ++j) {
                if (inBox(x + j, y, rows, cols)) {
                    lay_point.push_back({ y, x + j, });
                }
                if (j == 2 * i - 1) {
                    last_x = x + j;
                    last_y = y + 1;
                }
            }
        }

        return lay_point;
    }
};

////////////////////////////////////////////////////////////////////////////////

int main() {

    Solution s;

    s.spiralMatrixIII(1, 4, 0, 0);

    return 0;
}

官方的

这个是官方给出的

class Solution {
    public int[][] spiralMatrixIII(int R, int C, int r0, int c0) {
        int[] dr = new int[]{0, 1, 0, -1};
        int[] dc = new int[]{1, 0, -1, 0};

        int[][] ans = new int[R*C][2];
        int t = 0;

        ans[t++] = new int[]{r0, c0};
        if (R * C == 1) return ans;

        for (int k = 1; k < 2*(R+C); k += 2)
            for (int i = 0; i < 4; ++i) {  // i: direction index
                int dk = k + (i / 2);  // number of steps in this direction
                for (int j = 0; j < dk; ++j) {  // for each step in this direction...
                    // step in the i-th direction
                    r0 += dr[i];
                    c0 += dc[i];
                    if (0 <= r0 && r0 < R && 0 <= c0 && c0 < C) {
                        ans[t++] = new int[]{r0, c0};
                        if (t == R * C) return ans;
                    }
                }
            }

        throw null;
    }
}

作者:LeetCode
链接:https://leetcode.cn/problems/spiral-matrix-iii/solution/luo-xuan-ju-zhen-iii-by-leetcode/
来源: 力扣 (LeetCode)
著作权归作者所有. 商业转载请联系作者获得授权, 非商业转载请注明出处.

总结

官方写的还是比我好多了, 将方向抽象成了枚举, 这样就不用写四段类似的的代码了, 值得我借鉴.

rainbow

这个人很懒,什么都没留下

文章评论