class Solution {
public:
void print(vector<vector<int>>& map, vector<int>& ans, int st1, int ed1, int st2, int ed2) {
if (st1 > ed1 || st2 > ed2) return;
if (st1 == ed1 && st2 == ed2) {
ans.push_back(map[st1][ed1]);
return;
}
if (st1 == ed1) {
for (int i = st2; i <= ed2; i++) ans.push_back(map[i][ed1]);
return;
}
if (st2 == ed2) {
for (int i = st1; i <= ed1; i++) ans.push_back(map[st2][i]);
return;
}
for (int i = st1; i <= ed1; i++) ans.push_back(map[st2][i]);
for (int i = st2 + 1; i <= ed2; i++) ans.push_back(map[i][ed1]);
for (int i = ed1 - 1; i >= st1; i--) ans.push_back(map[ed2][i]);
for (int i = ed2 - 1; i >= st2 + 1; i--) ans.push_back(map[i][st1]);
print(map, ans, st1 + 1, ed1 - 1, st2 + 1, ed2 - 1);
return;
}
vector<int> printMatrix(vector<vector<int> > matrix) {
int row = matrix.size();
int col = matrix[0].size();
vector<int> ans;
print(matrix, ans, 0, col - 1, 0, row - 1 );
return ans;
}
}; 相当于就是将map换成n,将ans换成printf,不知道怎么搞