这题也没有什么好的解法,在我看来就是每次从左下角转一圈,一直循环,直到结果达到一定的数目。附代码:
class Solution(object):
def spiralMatrixIII(self, R, C, r0, c0):
"""
:type R: int
:type C: int
:type r0: int
:type c0: int
:rtype: List[List[int]]
"""
start_x = r0
start_y = c0
result = [[r0,c0]]
if c0 + 1 < C:
result.append([r0,c0+1])
if r0 + 1 < R:
result.append([r0+1,c0+1])
if r0 + 1 < R:
result.append([r0+1, c0])
r0 += 1
c0 -= 1
step = 2
while len(result) < R*C :
if 0<= r0 < R and 0<= c0 < C:
result.append([r0,c0])
if len(result) >= R*C:
return result
for i in range(step):
r0 -= 1
if 0 <= r0 < R and 0 <= c0 < C:
result.append([r0,c0])
if len(result) >= R*C:
return result
for i in range(step+1):
c0 += 1
if 0 <= r0 < R and 0 <= c0 < C:
result.append([r0,c0])
if len(result) >= R*C:
return result
for i in range(step+1):
r0 += 1
if 0 <= r0 < R and 0 <= c0 < C:
result.append([r0,c0])
if len(result) >= R*C:
return result
for i in range(step+1):
c0 -= 1
if 0 <= r0 < R and 0 <= c0 < C:
result.append([r0,c0])
if len(result) >= R*C:
return result
step += 2
c0 -= 1
return result