Clean python code


  • 1

    Traversal circle and circle from side to center, and after one circle reset start point, width and height.

    class Solution(object):
        def spiralOrder(self, matrix):
            if not matrix:
                return []
            
            # start point is (row, col)
            row, col, width, height, r = 0, 0, len(matrix[0]), len(matrix), []
            while width > 0 and height > 0:
                i = j = 0
                for index, iterator in enumerate([xrange(width), xrange(1, height), [[], xrange(width - 2, -1, -1)][height != 1], [[], xrange(height - 2, 0, -1)][width != 1]]):
                    for x in iterator:
                        if index & 1:
                            i = x
                        else:
                            j = x
                        r.append(matrix[row + i][col + j])
                row += 1
                col += 1
                width -= 2
                height -= 2
            return r

Log in to reply
 

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