• Spiral Matrix


    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


    The description of the problem

    The simplified version:
    Given a n × n n \times n n×n matrix, output its elements in spiral and chronological order.

    Given a m × n m \times n m×n matrix, return all elements of the matrix in spiral order.

    一、The codes 1 for problem_!

    #include 
    #include 
    using namespace std;
    class Solution {
    public:
        vector<int> spiralOrder(vector<vector<int>>& matrix) {
            vector<int> res;
            int start_x = 0, start_y = 0;
            int offset = 1;
            int n = matrix.size();
            int i, j;
            for (int k = 0; k < n / 2; k++) {
                for (j = start_y; j < n - offset; j++) {
                    res.push_back(matrix[start_x][j]);
                }
                for (i = start_x; i < n - offset; i++) {
                    res.push_back(matrix[i][j]);
                }
                for (; j > start_y; j--) {
                    res.push_back(matrix[i][j]);
                }
                for (; i > start_x; i--) {
                    res.push_back(matrix[i][j]);
                }
                start_x = start_x + 1;
                start_y = start_y + 1;
                offset++;
            }
            if (n % 2) {
                res.push_back(matrix[n/2][n/2]);
            }
            return res;
        }   
    };
    int main()
    {
        Solution s;
        vector<vector<int>> matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
        vector<int> res = s.spiralOrder(matrix);
        for (int i = 0; i < res.size(); i++) {
            cout << res[i] << " ";
        }
        cout << endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    The codes for the second problem

    #include 
    #include 
    using namespace std;
    class Solution {
    public:
        vector<int> spiralOrder(vector<vector<int>>& matrix) {
            vector<int> res;
            int m = matrix.size();
            int n = matrix[0].size();
            int start_x = 0, start_y = 0;
            int offset = 1;
            int i = 0, j = 0;
            int layers = m < n ? m : n;
            layers = layers / 2;
            for (int k = 0; k < layers; k++) {
                for (j = start_y; j < n - offset; j++) {
                    res.push_back(matrix[start_x][j]);
                }
                for (i = start_x; i < m - offset; i++) {
                    res.push_back(matrix[i][j]);
                }
                for (; j > start_y; j--) {
                    res.push_back(matrix[i][j]);
                }
                for (; i > start_x; i--) {
                    res.push_back(matrix[i][j]);
                }
                start_x++;
                start_y++;
                offset++;
            }
            if ((m <= n) && (m % 2 == 1)) {
                for (int k = 0; k < (n - 2*layers); k++) {
                    res.push_back(matrix[start_x][start_y + k]);
                }
            } else if ((n < m) && (n % 2 == 1)) {
                for (int k = 0; k < (m - 2*layers); k++) {
                    res.push_back(matrix[start_x + k][start_y]);
                }
            }
            return res;
        }
    };
    int main() {
        vector<vector<int>> matrix = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 16}
        };
        Solution s;
        vector<int> res = s.spiralOrder(matrix);
        for (int i = 0; i < res.size(); i++) {
            cout << res[i] << " ";
        }
        cout << endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    conclusion

    know your variables and use them correctly.

  • 相关阅读:
    ArcGIS 软件中路网数据的制作,手把手教学
    六种正确清理C盘的方法,解决你的红色烦恼
    Lucene、Solr和Elasticsearch三者的区别与联系
    Pytorch2.0发布了,向下兼容,加一句代码,性能翻番
    自动化测试报告
    Vue 源码解读(5)—— 全局 API
    在c#中使用CancellationToken取消任务
    Python数据容器
    指针笔试题讲解
    Python+超市进销存 毕业设计-附源码211549
  • 原文地址:https://blog.csdn.net/weixin_38396940/article/details/126180363