标签:number public 输入 span back 个数 column size turn
class Solution { public: vector<int> printMatrix(vector<vector<int>> matrix) { vector<int> res; if(matrix.size()==0) return res; int start = 0; int rows = matrix.size(); int columns = matrix[0].size(); while(columns > start*2 && rows > start *2){ printCircle(matrix,columns,rows,start,res); ++ start; } return res; } void printCircle(vector<vector<int>> &matrix,int columns,int rows,int start,vector<int> & res){ int endX = columns - 1 - start; int endY = rows - 1 - start; for(int i = start; i <= endX; ++i){ int number = matrix[start][i]; res.push_back(number); } if(start < endY){ for(int i = start+1;i<=endY;++i){ int number = matrix[i][endX]; res.push_back(number); } } if(start < endX && start < endY){ for(int i = endX-1;i >= start;--i){ int number = matrix[endY][i]; res.push_back(number); } } if(start < endX && start < endY-1){ for(int i = endY -1;i>= start +1;--i){ int number = matrix[i][start]; res.push_back(number); } } } };
标签:number public 输入 span back 个数 column size turn
原文地址:http://www.cnblogs.com/xiuxiu55/p/6486951.html