A recursive way to compute


  • 0
    A
    class Solution(object):
        def spiralOrder(self, matrix):
        	if not matrix:
        		return []
        	
        	M = len(matrix)
        	N = len(matrix[0])
        	if M == 1:
        		return matrix[0]
        	if N == 1:
        		return [matrix[i][0] for i in xrange(M)]
        	res = []
        	res.extend(matrix[0])
        	res.extend([matrix[i][N-1] for i in xrange(1, M)])
        	res.extend([matrix[M-1][j] for j in xrange(N-2, -1, -1)])
        	res.extend([matrix[i][0] for i in xrange(M-2, 0, -1)])
        	
        	if M > 2 and N > 2:
        		
        		return res + self.spiralOrder([ [matrix[i][j] for j in xrange(1, N-1)] for i in xrange(1, M-1)])
        	else:
        		return res 
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.