class Solution {
public:
vector<int> printMatrix(vector<vector<int> > matrix) {
bool fangxiang = true;
int x = 0, y = 0;
vector<int> result;
while(matrix.size() != 0){
if(matrix[0].size() == 0)
break;
if(fangxiang){
if(y <= 0){
y = y == 0? matrix[x].size()-1 : 0;
while(matrix[x].size()){
result.push_back(matrix[x][0]);
matrix[x].erase(matrix[x].begin());
}
matrix.erase(matrix.begin());
} else if(y >= matrix[x].size()-1){
y = 0;
while(matrix[x].size()){
result.push_back(matrix[x][matrix[x].size()-1]);
matrix[x].erase(matrix[x].end()-1);
}
matrix.erase(matrix.end()-1);
}
fangxiang = false;
}else{
if(x <= 0){
x = matrix.size() -1;
for(int i = 0; i < matrix.size(); i++){
result.push_back(matrix[i][matrix[i].size()-1]);
matrix[i].erase(matrix[i].end()-1);
}
}else{
x = 0;
for(int i = matrix.size()-1; i >=0; i--){
result.push_back(matrix[i][0]);
matrix[i].erase(matrix[i].begin());
}
}
fangxiang = true;
}
}
return result;
}
};