- 注意注释的细节
class Solution { public: vector<int> printMatrix(vector<vector<int> > matrix) { if(!matrix.size()) return {}; int top = 0, bottom = matrix.size()-1; int left = 0, right = matrix[0].size()-1; vector<int> res; while(top< (matrix.size()+1)/2&& left< (matrix[0].size()+1)/2){//向上取整小于中点 //top for(int i = left; i<= right; i++){ res.emplace_back(matrix[top][i]); } //right for(int i= top +1; i<=bottom;i++){//注意索引top + 1 res.emplace_back(matrix[i][right]); } //bottom for(int i = right-1; i>= left&&bottom!=top; i--){//除了注意索引,上下不能碰 res.emplace_back(matrix[bottom][i]); } //left for(int i = bottom-1; i>= top+1&&left!=right; i--){//除了注意索引,左右不能碰 res.emplace_back(matrix[i][left]); } top++;bottom--;left++;right--; } return res; } };