Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution(object):
- def spiralOrder(self, matrix):
- """
- :type matrix: List[List[int]]
- :rtype: List[int]
- """
- res = []
- if not matrix:
- return res
- rBegin = 0
- rEnd = len(matrix)-1
- cBegin = 0
- cEnd = len(matrix[0])-1
- while rBegin <= rEnd and cBegin <= cEnd:
- # traversal right
- for i in range(cBegin, cEnd+1):
- res.append(matrix[rBegin][i])
- rBegin += 1
- # traversal down
- for i in range(rBegin, rEnd+1):
- res.append(matrix[i][cEnd])
- cEnd -= 1
- # traversal left
- if rBegin <= rEnd:
- for i in range(cEnd, cBegin-1, -1):
- res.append(matrix[rEnd][i])
- rEnd -= 1
- # traversal up
- if cBegin <= cEnd:
- for i in range(rEnd, rBegin-1, -1):
- res.append(matrix[i][cBegin])
- cBegin += 1
- return res
Advertisement
Add Comment
Please, Sign In to add comment