Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 898d1a4

Browse files
Create spiralMatrix.py
1 parent d4a519b commit 898d1a4

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎Kangli/Arrays/spiralMatrix.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution(object):
2+
def spiralOrder(self, matrix):
3+
if not matrix or not matrix[0]:
4+
return []
5+
res = []
6+
colBegin, colEnd, rowBegin, rowEnd = 0, len(matrix[0])-1, 0, len(matrix)-1
7+
8+
while colBegin <= colEnd and rowBegin <= rowEnd:
9+
for i in range(colBegin, colEnd+1):
10+
res.append(matrix[rowBegin][i])
11+
rowBegin += 1
12+
13+
for i in range(rowBegin, rowEnd+1):
14+
res.append(matrix[i][colEnd])
15+
colEnd -= 1
16+
if rowBegin <= rowEnd:
17+
for i in range(colEnd, colBegin-1, -1):
18+
res.append(matrix[rowEnd][i])
19+
rowEnd -= 1
20+
21+
if colBegin <= colEnd:
22+
for i in range(rowEnd, rowBegin-1, -1):
23+
res.append(matrix[i][colBegin])
24+
colBegin += 1
25+
26+
return res
27+
28+
29+
30+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /