• 顺时针打印矩阵(Python and C++解法)


    题目:

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

    示例 1:

    输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
    输出:[1,2,3,6,9,8,7,4,5]
    示例 2:

    输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
    输出:[1,2,3,4,8,12,11,10,9,5,6,7]

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof

    思路:

    A00  A01  A02  A03

    D10  E11  E12  B13

    D20  G21  F22    B23

    C30    C31  C32  B33 

    第一圈的起点(0,0),第二圈的起点(1,1)...

    打印循环终止条件:对于一个n*m矩阵,存在2*star < n and 2*start <m

    每一行每一列的打印前提条件见C++注释。

    C++解法:

     1 class Solution {
     2 public:
     3     vector<int> spiralOrder(vector<vector<int>>& matrix) {
     4         if (matrix.size() == 0)  return {};  // 返回空的方法
     5         int rows = matrix.size(), columns = matrix[0].size();
     6         vector<int> orderResult;  // 存储结果
     7         int start = 0;  // 起点
     8 
     9         while (rows > 2 * start && columns > 2 * start) {  // 打印终止的条件
    10             int endX = columns - 1 - start;  // 终止行号
    11             int endY = rows - 1 - start;  // 终止列号
    12 
    13             // 从左到右打印,这一步总会有 
    14             for (int i = start; i <= endX; i++)
    15                 orderResult.push_back(matrix[start][i]);
    16             // 判断是否需要从上到下打印,终止行号需要大于起始行号
    17             if (start < endY)
    18                 for (int i = start + 1; i <= endY; i++)  // 从上到下打印
    19                     orderResult.push_back(matrix[i][endX]);
    20             // 判断是否需要从右到左打印,终止行/列号都需要大于起始行/列号,即至少两行两列
    21             if (start < endX && start < endY)
    22                 for (int i = endX - 1; i >= start; i--)
    23                     orderResult.push_back(matrix[endY][i]);
    24             // 判断是否需要从下到上打印,不仅终止列号需要大于起始列号,终止行号也要比起始行号大2,即至少三行两列
    25             if (start < endX && start < endY - 1)
    26                 for (int i = endY - 1; i >= start + 1; i--)
    27                     orderResult.push_back(matrix[i][start]);
    28 
    29             start += 1;
    30         }
    31         return orderResult;
    32     }
    33 };

    Python解法:

     1 class Solution:
     2     def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
     3         if len(matrix) == 0:
     4             return []
     5         rows = len(matrix)
     6         colums = len(matrix[0])
     7         start = 0
     8         orderResult = []
     9 
    10         while rows > 2 * start and colums > 2 * start:
    11             endX = colums - 1 - start
    12             endY = rows - 1 - start
    13 
    14             for i in range(start, endX+1):  #  从左到右打印
    15                 orderResult.append(matrix[start][i])
    16 
    17             if endY > start:  # 从上到下打印
    18                 for i in range(start + 1, endY+1):
    19                     orderResult.append(matrix[i][endX])
    20 
    21             if endX > start and endY > start:  # 从右到左打印
    22                 for i in range(endX - 1, start - 1, -1):
    23                     orderResult.append(matrix[endY][i])
    24 
    25             if endX > start and endY - 1 > start:  # 从下到上打印
    26                 for i in range(endY - 1, start, -1):
    27                     orderResult.append(matrix[i][start])
    28 
    29             start += 1
    30         return orderResult
  • 相关阅读:
    在redhat 6.6上安装Docker
    Linux操作系统各版本ISO镜像下载(包括oracle linux edhatcentosu
    UML时序图(Sequence Diagram)学习笔记
    eureka 和zookeeper 区别 优势【转】
    HttpClient实现远程调用
    Java 1.8 Stream 用例测试
    ZooKeeper下载安装(Windows版本)
    Java1.8新特性
    mysql大数据量表索引与非索引对比
    druid监控mysql程序
  • 原文地址:https://www.cnblogs.com/kongzimengzixiaozhuzi/p/13264031.html
Copyright © 2020-2023  润新知