剑指offer--面试题20:顺时针打印矩阵

    xiaoxiao2023-03-24  2

    

    题目描述

    输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 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. python实现: # -*- coding:utf-8 -*- class Solution:     # matrix类型为二维列表,需要返回列表     def printMatrix(self, matrix):         # write code here         m = len(matrix)         n = len(matrix[0]) if m else 0         result = []         if m==0 or n==0:             return result         result += matrix[0]         i, j = 0, n-1#第一行最后一个元素         stepRow, stepCol = n, m#横着走和竖着走的步数         direction = 1#1(下),2(左),3(上),4(右)         while stepRow and stepCol:             if direction==1:                 stepCol -= 1                 for _ in range(stepCol):                     i += 1                     result.append(matrix[i][j])                 direction = 2                               elif direction==2:                 stepRow -= 1                 for _ in range(stepRow):                     j -= 1                     result.append(matrix[i][j])                 direction = 3                               elif direction==3:                 stepCol -= 1                 for _ in range(stepCol):                     i -= 1                     result.append(matrix[i][j])                 direction = 4                               elif direction==4:                 stepRow -= 1                 for _ in range(stepRow):                     j += 1                     result.append(matrix[i][j])                 direction = 1             else:                 pass         return result c++实现: class Solution { public:     vector<int> printMatrix(vector<vector<int>> matrix) {         int m = matrix.size();         vector<int> result;         if(m==0)             return result;         int n = matrix[0].size();         if(n==0)             return result;                   for(auto item: matrix[0]){             result.push_back(item);         }                   int i=0, j=n-1;         int stepRow=n, stepCol=m;         int direction = 1;         while(stepRow && stepCol){             if(direction==1){                 stepCol--;                 for(int k=0;k<stepCol;k++){                     result.push_back(matrix[++i][j]);                 }                 direction = 2;             }else if(direction==2){                 stepRow--;                 for(int k=0;k<stepRow;k++){                     result.push_back(matrix[i][--j]);                 }                 direction = 3;             }else if(direction==3){                 stepCol--;                 for(int k=0;k<stepCol;k++){                     result.push_back(matrix[--i][j]);                 }                 direction = 4;             }else if(direction==4){                 stepRow--;                 for(int k=0;k<stepRow;k++){                     result.push_back(matrix[i][++j]);                 }                 direction = 1;             }//end if         }//end while         return result;     }//end printMatrix };
    转载请注明原文地址: https://ju.6miu.com/read-1200109.html
    最新回复(0)