LeetCode-Python-54. 螺旋矩阵

给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

示例 1:

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

示例 2:

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

第一种思路:

模拟法,直接模拟螺旋的顺序,用一个变量state 来作为控制方向的依据。

每次走过的点,就置为-999999标记来过了。

class Solution(object):
    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        m = len(matrix)
        if m == 0:
            return []
        n = len(matrix[0])
        if n == 0:
            return []
        x, y = 0,0
        cnt = 1
        state = "right"
        res = list()
        while(cnt <= m * n):
            # print x, y,state
            res.append(matrix[x][y])
            matrix[x][y] = -999999 #标记来过
            cnt += 1
            if state == "right":
                if y != n - 1 and matrix[x][y + 1] != -999999: #可以向右走
                    y += 1
                    continue
                else:#要向下转了
                    if x + 1 < m and matrix[x + 1][y] != -999999: #可以向下
                        x += 1
                        state = "down"
                        continue
            
            elif state == "down":
                if x != m - 1 and matrix[x + 1][y] != -999999: #可以向下走
                    x += 1
                    continue
                else:#要向左转
                    if y > 0 and matrix[x][y - 1] != -999999:
                        y -= 1
                        state = "left"
                        continue
            
            elif state == "left":
                if y != 0 and matrix[x][y - 1] != -999999:
                    y -= 1
                    continue
                else:#要向上走
                    if x != 0 and matrix[x - 1][y] != -999999:
                        x -= 1
                        state = "up"
                        continue
                        
            elif state == "up":
                if x != 0 and matrix[x - 1][y] != -999999:
                    x -= 1
                    continue
                else:#右转
                    if y != n -1 and matrix[x][y + 1] != -999999:
                        y += 1
                        state = "right"
                        continue
                        
        # print res
        return res
                    
                    
                
                        
                
                    
        

第二种思路:

还是模拟法,但是用方向变量的改变值di, dj来控制方向。

class Solution(object):
    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        m = len(matrix)
        if m == 0:
            return []
        n = len(matrix[0])
        if n == 0:
            return []
        r, i, j, di, dj = list(), 0, 0, 0, 1
        
        for _ in range(m * n):
            r.append(matrix[i][j])
            matrix[i][j] = None
            if matrix[(i + di) % m][(j + dj) % n] == None: #来过了改转向了
                di, dj = dj, -di #0 1 变 1 0, 1 0 变 0 -1, 0 -1 变 -1, 0, -1 0 变 0 1                
            i += di
            j += dj

        return r
                  

猜你喜欢

转载自blog.csdn.net/qq_32424059/article/details/89035106