题解 | #顺时针打印矩阵#

顺时针打印矩阵

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;
    }
};

全部评论

相关推荐

能干的三文鱼刷了100道题:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
04-25 18:13
五邑大学 Java
后来123321:大二两段实习太厉害了,我现在大二连面试都没有
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务