【C++刷题笔记】螺旋矩阵的两种写法

螺旋矩阵有两种判断大循环结束的方式,第一种是判断需要循环多少次,奇数的话需要额外处理;第二种通过取多少个数判断,不需要额外处理

方法一:

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<vector<int>> &v = matrix;
        vector<int> ret;
        int x = 0, y = 0;   // x表示当前这一圈的最左上角的纵向起始坐标,y表示横向起始坐标
        int w = v[0].size();    // w表示矩阵当前最左边的边界
        int h = v.size();   // h表示矩阵做下面的边界
        int n = min(w,h) / 2;
        int h1 = h;
        int w1 = w;
        while (n--) {
            int i = x, j = y;   // i表示纵向坐标,j表示横向坐标
            for (; j < w; ++j) {
                // cout << v[i][j] << " ";
                ret.push_back(v[i][j]);
            }
            --j;
            ++i;
            for ( ; i < h; ++i) {
                // cout << v[i][j] << " ";
                ret.push_back(v[i][j]);
            }
            --i;
            --j;
            for (; j >= y; --j) {
                // cout << v[i][j] << " ";
                ret.push_back(v[i][j]);
            }
            ++j;
            --i;
            for ( ; i > x; --i) {
                // cout << v[i][j] << " ";
                ret.push_back(v[i][j]);
            }
            x = x + 1;
            y = y + 1;
            w -= 1;
            h -= 1;
        }
        if (w1 < h1 && w1 %2 == 1) {
            for (int i = x; i < h; ++i) {
                // cout << v[i][y] << " ";
                ret.push_back(v[i][y]);
            }
        }
        if (h1 < w1 && h1 % 2 == 1) {
            for (int i = y; i < w; ++i) {
                // cout << v[x][i] << " ";
                ret.push_back(v[x][i]);
            }
        }
        if (h1 == w1 && h1 %2 == 1) {
            // cout << v[x][y] << " ";
            ret.push_back(v[x][y]);
        }
        return ret;
        // cout << endl;
    }
};

第二种:

#include "iostream"

using namespace std;

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<int> ret;
        int m = matrix.size();
        int n = matrix[0].size();
        int x = 0, y = 0;
        int startX = 0, startY = 0;
        int count = 1;
        int sum = matrix.size() * matrix[0].size();
        while (count <= sum) {
            x = startX, y = startY;
            while (count <= sum && y < n) {
                ret.push_back(matrix[x][y]);
                count++;
                y++;
            }
            y--;
            x++;
            while (count <= sum && x < m) {
                ret.push_back(matrix[x][y]);
                x++;
                count++;
            }
            x--;
            y--;
            while (count <= sum && y >= startY) {
                ret.push_back(matrix[x][y]);
                y--;
                count++;
            }
            y++;
            x--;
            while (count <= sum && x > startX) {
                ret.push_back(matrix[x][y]);
                x--;
                count++;
            }
            m--;
            n--;
            startX = startX + 1;
            startY = startY + 1;
        }
        return ret;
    }
};

你可能感兴趣的:(刷题,C++,c++,笔记,矩阵)