题解 | #顺时针打印矩阵#
顺时针打印矩阵
https://www.nowcoder.com/practice/9b4c81a02cd34f76be2659fa0d54342a
class Solution { public: vector<int> printMatrix(vector<vector<int> > matrix) { int i = 0, j = 0; vector<int> v; while (v.size() < matrix.size()*matrix[0].size()) { //右 if (matrix[i][j] == -1) ++j; while (j < matrix[i].size() && matrix[i][j] != -1) { v.push_back(matrix[i][j]); matrix[i][j] = -1; ++j; } --j; //下 if (matrix[i][j] == -1) ++i; while (i < matrix.size() && matrix[i][j] != -1) { v.push_back(matrix[i][j]); matrix[i][j] = -1; ++i; } --i; //左 if (matrix[i][j] == -1) --j; while (j >= 0 && matrix[i][j] != -1) { v.push_back(matrix[i][j]); matrix[i][j] = -1; --j; } ++j; //上 if (matrix[i][j] == -1) --i; while (i >= 0 && matrix[i][j] != -1) { v.push_back(matrix[i][j]); matrix[i][j] = -1; --i; } ++i; } return v; } };