54. Spiral Matrix

54. Spiral Matrix

题目:
https://leetcode.com/problems/spiral-matrix/

难度:
Medium

参考别人的代码,一开始觉得很有递归性,根据奇偶不同来写,递归太难写。

然后想到了loop,再想,可能有更优trick,事实证明并没有。

用四个变量来控制边界,然后因为方向总是:→↓←↑ 左右下上

class Solution(object):
    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        if matrix == [] : return []
        res = []
        maxUp = maxLeft = 0
        maxDown = len(matrix) - 1
        maxRight = len(matrix[0]) - 1 
        direction = 0 # 0 go right, 1 go down, 2 go left, 3 up
        while True:
            if direction == 0: #go right
                for i in range(maxLeft, maxRight+1):
                    res.append(matrix[maxUp][i])
                maxUp += 1
            elif direction == 1: # go down
                for i in range(maxUp, maxDown+1):
                    res.append(matrix[i][maxRight])
                maxRight -= 1
            elif direction == 2: # go left
                for i in reversed(range(maxLeft, maxRight+1)):
                    res.append(matrix[maxDown][i])
                maxDown -= 1
            else: #go up
                for i in reversed(range(maxUp, maxDown+1)):
                    res.append(matrix[i][maxLeft])
                maxLeft +=1
            if maxUp > maxDown or maxLeft > maxRight:
                return res
            direction = (direction + 1 ) % 4 

你可能感兴趣的:(54. Spiral Matrix)