Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[矩阵] #7

Open
Linjiayu6 opened this issue Jun 20, 2020 · 0 comments
Open

[矩阵] #7

Linjiayu6 opened this issue Jun 20, 2020 · 0 comments

Comments

@Linjiayu6
Copy link
Owner

1 - 打印矩阵

剑指 Offer 29. 顺时针打印矩阵

54. 螺旋矩阵

class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        m = len(matrix)
        if m == 0: return []
        if m == 1: return matrix[0] # 矩阵只有一行
        if m == 2: return matrix[0] + matrix[1][::-1] # 矩阵只有两行

        if len(matrix[0]) == 0: return [] # 矩阵一行里没有值
        if len(matrix[0]) == 1: # 矩阵一行里只有一个值
            result = []
            for m in matrix:
                result += m
            return result
        
        up = matrix.pop(0) # 第一个
        bottom = matrix.pop() # pop最后一个
        left, right = [], []
        for i in range(len(matrix)):
                left.append(matrix[i].pop(0))
                right.append(matrix[i].pop())

        # [::-1] 数组逆序
        return up + right + bottom[::-1] + left[::-1] + self.spiralOrder(matrix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant