美文网首页数据结构和算法
剑指offer - 顺时针打印矩阵

剑指offer - 顺时针打印矩阵

作者: Longshihua | 来源:发表于2019-08-05 09:14 被阅读0次

    题目

    输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。例如,如果输入如下矩阵

    2019-05-17-162435.jpg

    算法实现

    #include <iostream>
    using namespace std;
    
    void PrintMatrixInCircle(int **numbers, int columns, int rows, int start);
    void printNumber(int number);
    
    // numbers 矩阵 columns列 rows行
    void PrintMatrixClockwisely(int **numbers, int columns, int rows) {
        if (numbers == nullptr || columns <= 0 || rows <= 0)
            return;
    
        int start = 0; // 开始位置
    
        while (columns > start*2 && rows > start*2) {
            PrintMatrixInCircle(numbers, columns, rows, start);
            ++start;
        }
    }
    
    void PrintMatrixInCircle(int **numbers, int columns, int rows, int start) {
        int endX = columns - 1 - start;
        int endY = rows - 1 - start;
    
        // 从左到右打印一行
        for (int i=start; i <= endX; ++i) {
            int number = numbers[start][i];
            printNumber(number);
        }
    
        // 从上到下打印一列
        if (start < endY) {
            for (int i = start + 1; i <= endY; ++i) {
                int number = numbers[i][endX];
               printNumber(number);
            }
        }
    
        // 从右到左打印一行
        if (start < endX && start < endY) {
            for (int i=endX - 1; i>=start; --i) {
                int number = numbers[endY][i];
                printNumber(number);
            }
        }
    
        // 从上到下打印一列
        if (start<endX && start < endY - 1) {
            for (int i = endY - 1; i >= start + 1; --i) {
                int number = numbers[i][start];
                printNumber(number);
            }
        }
    }
    
    void printNumber(int number)
    {
        printf("%d\t", number);
    }
    

    使用vector实现

    vector<int> printMatrix(vector<vector<int> > matrix) {
       unsigned long row = matrix.size();
       unsigned long col = matrix[0].size();
       vector<int> res;
    
        // 输入的二维数组非法,返回空的数组
        if (row == 0 || col == 0)  return res;
    
        // 定义四个关键变量,表示左上和右下的打印范围
        int left = 0, top = 0, right = col - 1, bottom = row - 1;
        while (left <= right && top <= bottom)
        {
            // left to right
            for (int i = left; i <= right; ++i)  res.push_back(matrix[top][i]);
            // top to bottom
            for (int i = top + 1; i <= bottom; ++i)  res.push_back(matrix[i][right]);
            // right to left
            if (top != bottom)
                for (int i = right - 1; i >= left; --i)  res.push_back(matrix[bottom][i]);
            // bottom to top
            if (left != right)
                for (int i = bottom - 1; i > top; --i)  res.push_back(matrix[i][left]);
    
            left++;
            top++;
            right--;
            bottom--;
        }
        return res;
    }
    

    参考

    《剑指offer》
    顺时针打印矩阵

    相关文章

      网友评论

        本文标题:剑指offer - 顺时针打印矩阵

        本文链接:https://www.haomeiwen.com/subject/hrusjqtx.html