Leetcode刷题记录 旋转矩阵

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
posted @ 2019-12-23 01:53  WESWES  阅读(265)  评论(0编辑  收藏  举报