列表

详情


NC266. 顺时针打印矩阵

描述

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵:
[[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]]
则依次打印出数字
[1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10]
数据范围:
0 <= matrix.length <= 100
0 <= matrix[i].length <= 100

示例1

输入:

[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]

输出:

[1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10]

示例2

输入:

[[1,2,3,1],[4,5,6,1],[4,5,6,1]]

输出:

[1,2,3,1,1,1,6,5,4,4,5,6]

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 2ms, 内存消耗: 556KB, 提交时间: 2021-10-02

class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        vector<int> ans;
        int m = matrix.size(), n = matrix[0].size();
        int left = 0, right = n - 1, top = 0, down = m - 1;
        while(true){
            for(int i = left; i <= right; i++){
                ans.push_back(matrix[top][i]);
            }
            if(++top > down ) break;
            for(int i = top; i <= down; i++){
                ans.push_back(matrix[i][right]);
            }
            if(--right < left) break;
            for(int i = right; i >= left; i--){
                ans.push_back(matrix[down][i]);
            }
            if(--down < top) break;
            for(int i = down; i >= top; i--){
                ans.push_back(matrix[i][left]);
            }
            if(++left > right) break;
        }
        return ans;
    }
};

C 解法, 执行用时: 3ms, 内存消耗: 552KB, 提交时间: 2022-02-10

int* printMatrix(int** matrix, int matrixRowLen, int* matrixColLen, int* returnSize ) {
    // write code here
    int pos=0, left = 0, right = *matrixColLen, top = 0, bot = matrixRowLen;
    int* res = malloc(right*bot*sizeof(int));
    while(left<right && top<bot){
        for(int i=left; i<right; ++i) res[pos++]=matrix[top][i];
        top++;
        for(int i=top; i<bot; ++i) res[pos++]=matrix[i][right-1];
        right--;
        if(left>=right || top>=bot) break;
        for(int i=right-1; i>=left; --i) res[pos++]=matrix[bot-1][i];
        bot--;
        for(int i=bot-1; i>=top;--i) res[pos++]=matrix[i][left];
        left++;
    }
    
    *returnSize = pos;
    return res;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 640KB, 提交时间: 2021-04-24

class Solution {
public:
    void print(int lx, int ly, int rx, int ry, vector<vector<int>> &matrix, vector<int> &ret) {
 
        for (int j=ly; j<=ry; ++j) ret.push_back(matrix[lx][j]);
        for (int i=lx+1; i<=rx; ++i) ret.push_back(matrix[i][ry]);
        int h = rx - lx + 1;
        if (h > 1)
            for (int rj=ry-1; rj>=ly; --rj) ret.push_back(matrix[rx][rj]);
        int w = ry - ly + 1;
        if (w > 1)
            for (int ri = rx-1; ri>=lx+1; --ri) ret.push_back(matrix[ri][ly]);
    }
    vector<int> printMatrix(vector<vector<int>>& matrix) {
        vector<int> ret;
        if (matrix.empty()) return ret;
        int lx = 0, ly = 0;
        int rx = matrix.size()-1, ry = matrix[0].size()-1;
        while (lx <= rx && ly <= ry) {
            print(lx++, ly++, rx--, ry--, matrix, ret);
        }
        return ret;
    }
 
};

C++ 解法, 执行用时: 3ms, 内存消耗: 644KB, 提交时间: 2021-11-13

class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        if(matrix.size()<=0){
            return {};
        }
        
        
        int left=0;
        int right=matrix[0].size()-1;
        int top=0;
        int down=matrix.size()-1;
        vector<int> ret;
        while(left<=right&&top<=down){
            if(top<=down){
                for(int i=left;i<=right;i++){
                    ret.push_back(matrix[top][i]);
                }
                top++;
            }
            if(left<=right){
                for(int i=top;i<=down;i++){
                    ret.push_back(matrix[i][right]);
                }
                right--;
            }
            
            if(top<=down){
                for(int i=right;i>=left;i--)
                    ret.push_back(matrix[down][i]);
                down--;
            }
            
            
            if(left<=right){
                for(int i=down;i>=top;i--)
                    ret.push_back(matrix[i][left]);
                left++;
            }
            
            
        }
        
        return ret;
        
    }
};

C++ 解法, 执行用时: 3ms, 内存消耗: 644KB, 提交时间: 2021-06-05

class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        vector<int> result;
        int n = matrix.size();
        int m = matrix[0].size();
        int upn = 0;
        int downn = n-1;
        int leftm =0;
        int rightm = m-1;
        while(upn < downn && leftm < rightm){
            for(int i =leftm; i<=rightm;++i)
                result.push_back(matrix[upn][i]);
            for(int i = upn+1; i<= downn;++i)
                result.push_back(matrix[i][rightm]);
            for(int i = leftm+1;i<=rightm;++i)
                result.push_back(matrix[downn][rightm-(i-leftm)]);
            for(int i = upn+1;i < downn;++i)
                result.push_back(matrix[downn-(i-upn)][leftm]);
            ++upn;
            --downn;
            ++leftm;
            --rightm;
        }
        if(downn > upn && leftm == rightm){
            for(int i = upn; i<=downn; ++i)
                result.push_back(matrix[i][leftm]);
        }
        if(downn == upn && leftm < rightm){
            for(int i = leftm; i<=rightm; ++i)
                result.push_back(matrix[upn][i]);
        }
        if(downn == upn && leftm == rightm)
            result.push_back(matrix[upn][leftm]);
        return result;
    }
};

上一题