Leetcode刷题记录 旋转矩阵

两盒软妹~` 提交于 2019-12-23 02:33:49

https://leetcode-cn.com/problems/spiral-matrix/submissions/

class Solution(object):
    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        if matrix == []:
            return []
        results = [matrix[0][0]]
        matrix[0][0] = None
        column = 0
        row = 0
        c_max = len(matrix[0]) - 1
        r_max = len(matrix) - 1
        while True:
            while column + 1 <= c_max and matrix[row][column + 1] != None:
                column += 1
                results.append(matrix[row][column])
                matrix[row][column] = None
            if row + 1 > r_max or matrix[row + 1][column] == None:
                break

            while row + 1 <= r_max and matrix[row + 1][column] != None:
                row += 1
                results.append(matrix[row][column])
                matrix[row][column] = None
            if column - 1 < 0 or matrix[row][column - 1] == None:
                break

            while column - 1 >= 0 and matrix[row][column - 1] != None:
                column -= 1
                results.append(matrix[row][column])
                matrix[row][column] = None
            if row - 1 < 0 or matrix[row - 1][column] == None:
                break

            while row - 1 >= 0 and matrix[row - 1][column] != None:
                row -= 1
                results.append(matrix[row][column])
                matrix[row][column] = None
            if column + 1 > c_max or matrix[row][column + 1] == None:
                break
        return results
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!