题解 | #顺时针打印矩阵#
顺时针打印矩阵
https://www.nowcoder.com/practice/97e7a475d2a84eacb60ee545597a8407
class Printer {
public:
vector<int> clockwisePrint(vector<vector<int> > mat, int n, int m) {
vector<int> ans;
int t = 1;
do {
for (int i = t - 1; i <= m - t - 1; i++) {
ans.push_back(mat[t - 1][i]);
}
for (int i = t - 1; i <= n - t - 1; i++) {
ans.push_back(mat[i][m - t]);
}
for (int i = m - t; i >= t; i--) {
ans.push_back(mat[n - t][i]);
}
for (int i = n - t; i >= t; i--) {
ans.push_back(mat[i][t - 1]);
}
t++;
} while (t * 2 <= n && t * 2 <= m);
if (n % 2 == 1 && n <= m) {
for (int i = t - 1; i <= m - t; i++) {
ans.push_back(mat[t - 1][i]);
}
}
if (m % 2 == 1 && m < n) {
for (int i = t-1; i <= n - t; i++) {
ans.push_back(mat[i][m - t]);
}
}
return ans;
}
};


