题解 | #顺时针移动牛#
顺时针移动牛
https://www.nowcoder.com/practice/30aeb37fc774493eac7f6b6b5bfd6660
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param matrix int整型vector<vector<>> * @return int整型vector */ vector<int> printMatrixInSpiralOrder(vector<vector<int> >& matrix) { // write code here int row = matrix.size(); vector<int> res; if(row == 0){ return res; } if(row == 1){ return matrix[0]; } int col = matrix[0].size(); int top = 0, bottom = row -1, left = 0, right = col-1; while(top <= bottom && left <= right){ //向右 for(int i = left; i<=right;i++){ res.emplace_back(matrix[top][i]); } top ++; //向下 for(int i = top; i<=bottom;i++){ res.emplace_back(matrix[i][right]); } right --; if(left<=right){ //向左 for(int i = right; i>=left;i--){ res.emplace_back(matrix[bottom][i]); } bottom --; //向上 for(int i = bottom; i>=top;i--){ res.emplace_back(matrix[i][left]); } left ++; } } return res; } };