本文共 960 字,大约阅读时间需要 3 分钟。
Solution1:
可参考题的解法 书上的思路特别好,学习之~class Solution {public: vector printMatrix(vector> matrix) { vector res; int rows = matrix.size(), cols = matrix[0].size(); if(rows == 0 || cols == 0) return res; int i = 0, j = 0;//循环变量 while(i <= rows-1-i && j <= cols-1-j){ PrintCycle(matrix, res, i, j, rows-1, cols-1); i++; j++; } return res; } void PrintCycle(vector > &matrix, vector &result, int m, int n, int row, int col){ int i = 0, j = 0; for(j=n;j<=col-n;j++) result.push_back(matrix[m][j]); for(i=m+1;i<=row-m;i++) result.push_back(matrix[i][col-n]); for(j=col-n-1;j>=n && m+1<=row-m;j--)//只有row-m行不与m行重合时才往回push_back result.push_back(matrix[row-m][j]); for(i=row-m-1;i>m && col-n-1>=n;i--)//只有n行不与col-n行重合时才往上push_back result.push_back(matrix[i][n]); }};
转载地址:http://uxhdb.baihongyu.com/