剑指OFFER----面试题29. 顺时针打印矩阵

链接:https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/

 

代码:

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<int> res;
        int n = matrix.size();
        if (!n) return res;
        int m = matrix[0].size();

        vector<vector<bool>> st(n, vector<bool>(m, false));
        int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
        int x = 0, y  = 0, d = 1;
        for (int i = 0; i < n * m; ++i) {
            res.push_back(matrix[x][y]);
            st[x][y] = true;
            int a = x + dx[d], b = y + dy[d];
            if (a < 0 || a >= n || b < 0 || b >= m || st[a][b]) {
                d = (d + 1) % 4;
                a = x + dx[d], b = y + dy[d];
            }
            x = a, y = b;
        }
        return res;
    }
};

 

posted @ 2020-02-26 10:51  景云ⁿ  阅读(198)  评论(0编辑  收藏  举报