/* 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,
如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出
class Solution {
public:
vector<int> printMatrix(vector<vector<int> > matrix) {
vector<int> value;
int row = matrix.size();
int col = matrix[0].size();
if(row != 0 && col != 0){
int left = 0, right = col - 1, top = 0, bottom = row - 1;
while(left <= right && top <= bottom){
for(int i = left; i <= right; ++ i){
value.push_back(matrix[top][i]);
}
if(top != bottom){
for(int i = top + 1; i <= bottom; ++ i){
value.push_back(matrix[i][right]);
}
if(left != right){
for(int i = right - 1; i >= left; -- i){
value.push_back(matrix[bottom][i]);
}
if(bottom - top > 1){
for(int i = bottom - 1; i > top; -- i){
value.push_back(matrix[i][left]);
}
}
}
}
++ left;
++ top;
-- right;
-- bottom;
}
}
return value;
}
};
数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.*/
/* 这道题需要画图来辅助:
首先,如果矩阵为空,那么我们返回一个空向量;
然后,如果矩阵不为空,那么矩阵至少含有一个元素;
画图:顺时针打印分为四步,依次为:从左至右打印,从上至下打印,从右至左打印,从下至上打印;我们设定
四个指标:left = 0, right = col - 1,top = 0,bottom = row - 1;
因为矩阵至少含有一个元素,所以从左至右第一行肯定可以打印出来;
第一步从左至右打印第一行;
然后判断:如果top = bottom,那么矩阵就只有一行,已经打印完了;如果top!=bottom,那么矩阵至少有两行,
可以开始第二步从上至下打印了,即行数从top+1到bottom,列数一直为right;
接着在top!=bottom的前提下,继续判断:如果left=right,那么矩阵就只有一列,那么现在已经打印完了;如果
left!=right,那么矩阵至少有两行两列,可以开始第三步从右至左打印了,即行数一直为bottom,列数为right-1
到left;
接着在top!=bottom以及left!=right的前提下,如果矩阵只有两行两列,那么现在也已经打印完了,所以第四步从
下至上打印的条件是,矩阵至少有3行2列,那么bottom-top>=2,这样就可以开始第四步打印了,行数从bottom-1到
top + 1, 列数一直为left。
*/