吃大大卷 | #顺时针打印矩阵#

顺时针打印矩阵

https://www.nowcoder.com/practice/9b4c81a02cd34f76be2659fa0d54342a?tpId=265&tqId=39231&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D13&difficulty=undefined&judgeStatus=undefined&tags=&title=

# -*- coding:utf-8 -*-
class Solution:
    # matrix类型为二维列表,需要返回列表
    def printMatrix(self, matrix):
        # write code here
        self.mat = matrix
        self.print_info = []
        while self.mat:
            if self.mat[0]:
                self.printTop()
                self.printRight()
                self.printBottom()
                self.printLeft()
            else:
                del self.mat[0]
        return self.print_info

    def printTop(self):
        if len(self.mat) == 0:
            return
        elif len(self.mat[0]) == 0:
            return
        for ele in self.mat[0]:
            self.print_info.append(ele)
        del self.mat[0]
    
    def printRight(self):
        if len(self.mat) == 0:
            return
        elif len(self.mat[0]) == 0:
            return
        for ele in self.mat:
            self.print_info.append(ele[-1])
            del ele[-1]
    
    def printBottom(self):
        if len(self.mat) == 0:
            return
        elif len(self.mat[0]) == 0:
            return
        for ele in self.mat[-1][::-1]:
            self.print_info.append(ele)
        del self.mat[-1]
    
    def printLeft(self):
        if len(self.mat) == 0:
            return
        elif len(self.mat[0]) == 0:
            return
        for ele in self.mat[::-1]:
            self.print_info.append(ele[0])
            del ele[0]

吃大大卷,拉出一截(打印),吃掉一截(删除)

全部评论

相关推荐

三年之期已到我的offer快到碗里来:9硕都比不上9本
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务