classSolution{public:vector<int>printMatrix(vector<vector<int>> matrix){vector<int> res;int n = matrix.size();//先排除特殊情况if(n ==0)return res;//左边界int left =0;//右边界int right = matrix[0].size()-1;//上边界int up =0;//下边界int down = n -1;//直到边界重合while(left <= right && up <= down){//上边界的从左到右for(int i = left; i <= right; i++) res.push_back(matrix[up][i]);//上边界向下up++;if(up > down)break;//右边界的从上到下for(int i = up; i <= down; i++) res.push_back(matrix[i][right]);//右边界向左right--;if(left > right)break;//下边界的从右到左for(int i = right; i >= left; i--) res.push_back(matrix[down][i]);//下边界向上down--;if(up > down)break;//左边界的从下到上for(int i = down; i >= up; i--) res.push_back(matrix[i][left]);//左边界向右left++;if(left > right)break;}return res;}};