标签:https scribe span circle tor void matrix coding target
1 class Solution { 2 public: 3 void printfCircle(vector<vector<int> > matrix,vector<int>& result,int start,int rows,int columns){ 4 int endRows = rows - start - 1; 5 int endColumns = columns - start - 1; 6 //打印第一行 7 for(int i = start;i <= endColumns;++i){ 8 result.push_back(matrix[start][i]); 9 } 10 //打印最后一列 11 if(start < endRows){ 12 for(int i = start + 1;i <= endRows;++i){ 13 result.push_back(matrix[i][endColumns]); 14 } 15 } 16 //打印最后一行 17 if(start < endRows && start < endColumns){ 18 for(int i = endColumns - 1;i >= start;--i){ 19 result.push_back(matrix[endRows][i]); 20 } 21 } 22 //打印第一列 23 if(start < endRows - 1 && start < endColumns){ 24 for(int i = endRows - 1;i >= start + 1;--i){ 25 result.push_back(matrix[i][start]); 26 } 27 } 28 } 29 vector<int> printMatrix(vector<vector<int> > matrix) { 30 vector<int> result; 31 if(matrix.empty()){ 32 return {}; 33 } 34 if(matrix[0].empty()){ 35 return {}; 36 } 37 int start = 0; 38 int rows = matrix.size(),columns = matrix[0].size(); 39 while(rows > 2*start && columns > 2*start){ 40 printfCircle(matrix,result,start,rows,columns); 41 ++start; 42 } 43 return result; 44 } 45 };
标签:https scribe span circle tor void matrix coding target
原文地址:http://www.cnblogs.com/dingxiaoqiang/p/7468309.html