class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
if(matrix.size() == 0) return {};
int up = 0,dow = matrix.size()-1,le = 0,ri = matrix[0].size()-1;
vector<int> res;
while(up<=dow || le <= ri){
for(int i=le;i<=ri && up <= dow;i++ ){
res.push_back(matrix[up][i]);
}
up++;
for(int i=up;i<=dow && ri >= le;i++ ){
res.push_back(matrix[i][ri]);
}
ri--;
for(int i=ri;i>=le && dow >= up;i-- ){
res.push_back(matrix[dow][i]);
}
dow--;
for(int i=dow;i>=up && le <= ri;i-- ){
res.push_back(matrix[i][le]);
}
le++;
}
return res;
}
};