题解 | #顺时针打印矩阵#
顺时针打印矩阵
http://www.nowcoder.com/practice/9b4c81a02cd34f76be2659fa0d54342a
class Solution {
public:
vector<int> printMatrix(vector<vector<int> > matrix) {
vector<int> arr;
int wa=0,wz=matrix[0].size()-1,ha=0,hz=matrix.size()-1;
int s=matrix.size()*matrix[0].size();
while(arr.size()<s){
for(int i=wa;i<=wz;i++){
arr.push_back(matrix[ha][i]);
}
if(arr.size()<s){
for(int i=ha+1;i<=hz;i++){
arr.push_back(matrix[i][wz]);
}
}
if(arr.size()<s){
for(int i=wz-1;i>=wa;i--){
arr.push_back(matrix[hz][i]);
}
}
if(arr.size()<s){
for(int i=hz-1;i>=ha+1;i--){
arr.push_back(matrix[i][wa]);
}
}
wz--;
hz--;
wa++;
ha++;
}
return arr;
}
};</int></int></int>