Created
January 15, 2020 08:38
-
-
Save c02y/ca086e400f1b1d375f534f07bf45a66d to your computer and use it in GitHub Desktop.
29. 顺时针打印矩阵
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# https://cyc2018.github.io/CS-Notes/#/notes/29.%20%E9%A1%BA%E6%97%B6%E9%92%88%E6%89%93%E5%8D%B0%E7%9F%A9%E9%98%B5 | |
class Solution: | |
def printMatrix(self, matrix): | |
res = [] | |
while matrix: | |
res += matrix.pop(0) | |
if matrix and matrix[0]: | |
for row in matrix: | |
res.append(row.pop()) | |
if matrix: | |
res += matrix.pop()[::-1] | |
if matrix and matrix[0]: | |
for row in matrix[::-1]: | |
res.append(row.pop(0)) | |
return res | |
if __name__ == '__main__': | |
matrix = [[1, 2, 3, 4], | |
[5, 6, 7, 8], | |
[9, 10, 11, 12], | |
[13, 14, 15, 16]] | |
print(Solution().printMatrix(matrix)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CPP version: