剑指offer67题-No19.顺时针打印矩阵

边界模拟 模拟四个边界情况,并不断收缩。

    vector<int> printMatrix(vector<vector<int> > matrix) {
        if(matrix.size() == 0) return {};
        if(matrix.size() == 1) return matrix.front();

        int l = 0;
        int r = matrix.front().size() -1 ;
        int top = 0;
        int bottom = matrix.size() -1;
        vector<int> res;
        while(l <= r && top <= bottom){
            // 顶层遍历
            for(int i = l; i <= r; ++i){
                res.push_back(matrix[top][i]);
            }
            if(++top > bottom) break;
            // 右侧遍历
            for(int i = top; i <= bottom; ++i){
                res.push_back(matrix[i][r]);
            }
            if(--r < l) break;
            // 底部遍历
            for(int i = r; i>=l; --i){
                res.push_back(matrix[bottom][i]);
            }
            if(--bottom < top) break;
            // 左侧遍历
            for(int i = bottom; i >=top; --i){
                res.push_back(matrix[i][l]);
            }
            if(++l > r) break;
        }
            return res;
    }
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇