Skip to content

Commit c1abde0

Browse files
committed
spiral matrix
1 parent aa6c664 commit c1abde0

File tree

4 files changed

+175
-118
lines changed

4 files changed

+175
-118
lines changed

053 Spiral Matrix.py

Lines changed: 75 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,75 @@
1-
"""
2-
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
3-
4-
For example,
5-
Given the following matrix:
6-
7-
[
8-
[ 1, 2, 3 ],
9-
[ 4, 5, 6 ],
10-
[ 7, 8, 9 ]
11-
]
12-
You should return [1,2,3,6,9,8,7,4,5].
13-
"""
14-
__author__ = 'Danyang'
15-
class Solution:
16-
def spiralOrder(self, matrix):
17-
"""
18-
top
19-
|
20-
left --+-- right
21-
|
22-
bottom
23-
24-
be careful with the index
25-
26-
:param matrix: a list of lists of integers
27-
:return: a list of integers
28-
"""
29-
if not matrix or not matrix[0]:
30-
return matrix
31-
32-
result = []
33-
34-
left = 0
35-
right = len(matrix[0])-1
36-
top = 0
37-
bottom = len(matrix)-1
38-
39-
while left<=right and top<=bottom:
40-
for i in xrange(left, right+1):
41-
result.append(matrix[top][i])
42-
for i in xrange(top+1, bottom+1):
43-
result.append(matrix[i][right])
44-
for i in reversed(xrange(left+1, right)):
45-
if top<bottom: # avoid double scanning the first row
46-
result.append(matrix[bottom][i])
47-
for i in reversed(xrange(top+1, bottom+1)):
48-
if left<right: # avoid double scanning the first column
49-
result.append(matrix[i][left])
50-
51-
left += 1
52-
right -= 1
53-
top += 1
54-
bottom -= 1
55-
56-
return result
57-
58-
if __name__=="__main__":
59-
print Solution().spiralOrder([[2, 3]])
1+
"""
2+
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
3+
4+
For example,
5+
Given the following matrix:
6+
7+
[
8+
[ 1, 2, 3 ],
9+
[ 4, 5, 6 ],
10+
[ 7, 8, 9 ]
11+
]
12+
You should return [1,2,3,6,9,8,7,4,5].
13+
"""
14+
__author__ = 'Danyang'
15+
16+
17+
class Solution:
18+
def spiralOrder(self, matrix):
19+
"""
20+
top
21+
|
22+
left --+-- right
23+
|
24+
bottom
25+
26+
t
27+
__
28+
l | | r
29+
|__|
30+
b
31+
32+
be careful with the index: be greedy for the first scan
33+
[[1,2,3],
34+
[8,9,4],
35+
[7,6,5]]
36+
37+
if not greedy, the middle 9 won't be scanned
38+
[[1,2,3],
39+
[8,x,4],
40+
[7,6,5]]
41+
42+
:param matrix: a list of lists of integers
43+
:return: a list of integers
44+
"""
45+
if not matrix or not matrix[0]:
46+
return matrix
47+
48+
result = []
49+
50+
left = 0
51+
right = len(matrix[0]) - 1
52+
top = 0
53+
bottom = len(matrix) - 1
54+
55+
while left <= right and top <= bottom:
56+
for c in xrange(left, right + 1):
57+
result.append(matrix[top][c])
58+
for r in xrange(top + 1, bottom + 1):
59+
result.append(matrix[r][right])
60+
for c in xrange(right - 1, left - 1, -1):
61+
if top < bottom: # avoid double scanning the first row
62+
result.append(matrix[bottom][c])
63+
for r in xrange(bottom - 1, top, -1):
64+
if left < right: # avoid double scanning the first column
65+
result.append(matrix[r][left])
66+
67+
left += 1
68+
right -= 1
69+
top += 1
70+
bottom -= 1
71+
72+
return result
73+
74+
if __name__=="__main__":
75+
print Solution().spiralOrder([[2, 3]])

058 Spiral Matrix II.py

Lines changed: 95 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,95 @@
1-
"""
2-
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
3-
4-
For example,
5-
Given n = 3,
6-
7-
You should return the following matrix:
8-
[
9-
[ 1, 2, 3 ],
10-
[ 8, 9, 4 ],
11-
[ 7, 6, 5 ]
12-
]
13-
"""
14-
__author__ = 'Danyang'
15-
16-
17-
class Solution:
18-
def generateMatrix(self, n):
19-
"""
20-
algorithm: array
21-
:param n: Integer
22-
:return: a list of lists of integer
23-
"""
24-
left = 0
25-
right = n-1 # [0, n)
26-
top = 0
27-
bottom = n-1 # [0, n)
28-
29-
result = [[-1 for _ in xrange(n)] for _ in xrange(n)]
30-
num = 1
31-
while left<=right and top<=bottom:
32-
for i in xrange(left, right+1): # tuning ending condition
33-
result[top][i] = num
34-
num += 1
35-
for i in xrange(top+1, bottom):
36-
result[i][right] = num
37-
num += 1
38-
39-
for i in xrange(right, left, -1):
40-
result[bottom][i] = num
41-
num += 1
42-
for i in xrange(bottom, top, -1):
43-
result[i][left] = num
44-
num += 1
45-
46-
left += 1
47-
right -= 1
48-
top += 1
49-
bottom -= 1
50-
51-
return result
52-
53-
54-
if __name__=="__main__":
55-
result = Solution().generateMatrix(4)
56-
for row in result:
57-
print row
1+
"""
2+
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
3+
4+
For example,
5+
Given n = 3,
6+
7+
You should return the following matrix:
8+
[
9+
[ 1, 2, 3 ],
10+
[ 8, 9, 4 ],
11+
[ 7, 6, 5 ]
12+
]
13+
"""
14+
__author__ = 'Danyang'
15+
16+
17+
class Solution:
18+
def generateMatrix(self, n):
19+
"""
20+
algorithm: array, simulation
21+
:param n: Integer
22+
:return: a list of lists of integer
23+
"""
24+
left = 0
25+
right = n - 1 # [0, n)
26+
top = 0
27+
bottom = n - 1 # [0, n)
28+
29+
result = [[-1 for _ in xrange(n)] for _ in xrange(n)]
30+
num = 1
31+
while left <= right and top <= bottom:
32+
for i in xrange(left, right + 1): # tuning ending condition, be greedy
33+
result[top][i] = num
34+
num += 1
35+
for i in xrange(top + 1, bottom):
36+
result[i][right] = num
37+
num += 1
38+
39+
for i in xrange(right, left, -1):
40+
result[bottom][i] = num
41+
num += 1
42+
for i in xrange(bottom, top, -1):
43+
result[i][left] = num
44+
num += 1
45+
46+
left += 1
47+
right -= 1
48+
top += 1
49+
bottom -= 1
50+
51+
return result
52+
53+
54+
class SolutionError:
55+
def generateMatrix(self, n):
56+
"""
57+
algorithm: array, simulation
58+
:param n: Integer
59+
:return: a list of lists of integer
60+
"""
61+
left = 0
62+
right = n - 1 # [0, n)
63+
top = 0
64+
bottom = n - 1 # [0, n)
65+
66+
result = [[-1 for _ in xrange(n)] for _ in xrange(n)]
67+
num = 1
68+
while left <= right and top <= bottom:
69+
for i in xrange(left, right): # tuning ending condition, this will fail in the middle
70+
result[top][i] = num
71+
num += 1
72+
for i in xrange(top, bottom):
73+
result[i][right] = num
74+
num += 1
75+
76+
for i in xrange(right, left, -1):
77+
result[bottom][i] = num
78+
num += 1
79+
80+
for i in xrange(bottom, top, -1):
81+
result[i][left] = num
82+
num += 1
83+
84+
left += 1
85+
right -= 1
86+
top += 1
87+
bottom -= 1
88+
89+
return result
90+
91+
92+
if __name__=="__main__":
93+
result = Solution().generateMatrix(4)
94+
for row in result:
95+
print row

068 Text Justification py3.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
9595
Round robin distribution of spaces
9696
9797
Look before jump
98+
Look before you leap
9899
"""
99100
ret = []
100101
char_cnt = 0
@@ -106,7 +107,7 @@ def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
106107
# break, move w into the next line
107108
# Round robin distribut the spaces except for the last word
108109
for i in range(maxWidth - char_cnt):
109-
cur_words[i % max(1, len(cur_words) - 1)] += " "
110+
cur_words[i % max(1, len(cur_words) - 1)] += " " # insert in between
110111
# len(cur_words) - 1 can be 0
111112
ret.append("".join(cur_words))
112113

697 Degree of an Array.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ def findShortestSubArray(self, nums: List[int]) -> int:
3636
return
3737

3838
counter = defaultdict(int)
39-
first = {}
39+
first = {} # map from number to index
4040
mx = [0, 0] # [degree, length]
4141
for i, n in enumerate(nums):
4242
if n not in first:
4343
first[n] = i # setdefault
4444
counter[n] += 1
4545
if counter[n] > mx[0]:
46+
# If there is only one mode number
4647
mx = [counter[n], i - first[n] + 1]
4748
elif counter[n] == mx[0]:
49+
# How to handle duplicate mode number
4850
mx[1] = min(mx[1], i - first[n] + 1)
4951

5052
return mx[1]

0 commit comments

Comments
 (0)