GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/054_Spiral_Matrix.py at master · ccphantom/leetcode

Expand file tree

/

054_Spiral_Matrix.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
47 lines (42 loc) · 1.66 KB
/

054_Spiral_Matrix.py

Copy path

File metadata and controls

47 lines (42 loc) · 1.66 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution(object):
def spiralOrder(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
if matrix is None or len(matrix) == 0:
return matrix
m, n = len(matrix), len(matrix[0])
return self.get_spiralOrder(matrix, 0, m - 1, 0, n - 1)
def get_spiralOrder(self, matrix, r_start, r_end, c_start, c_end):
if r_start > r_end or c_start > c_end:
return []
elif r_start == r_end:
return matrix[r_start][c_start:c_end + 1]
elif c_start == c_end:
return [matrix[j][c_end] for j in range(r_start, r_end + 1)]
curr = matrix[r_start][c_start:c_end + 1] + [matrix[j][c_end] for j in range(r_start + 1, r_end)] +\
matrix[r_end][c_start:c_end + 1][::-1] +\
[matrix[j][c_start] for j in reversed(range(r_start + 1, r_end))]
res = curr + self.get_spiralOrder(matrix, r_start + 1, r_end - 1, c_start + 1, c_end - 1)
return res
# def spiralOrder(self, matrix):
# res = []
# if not matrix:
# return []
# i, j, di, dj = 0, 0, 0, 1
# m, n = len(matrix), len(matrix[0])
# for v in xrange(m * n):
# res.append(matrix[i][j])
# matrix[i][j] = ''
# if matrix[(i + di) % m][(j + dj) % n] == '':
# # (0, 1) -> (1, 0) -> (0, -1) -> (-1, 0)
# # then loop
# di, dj = dj, -di
# i += di
# j += dj
# return res
if __name__ == '__main__':
# begin
s = Solution()
print s.spiralOrder([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.