python - spiral matrix

  • 0
    class Solution(object):
        def spiralOrder(self, matrix):
            :type matrix: List[List[int]]
            :rtype: List[int]
            rows = len(matrix)
            cols = len(matrix[0]) if rows else 0
            n = rows*cols
            start = 0
            answer = []
                # keep a count n of number of elts in answer
                # start from top left corner
                i, j = start, start
                while n and j < cols:
                    n, j = n-1, j+1
                i, j = i+1, j-1
                while n and i < rows:
                    n, i = n-1, i+1
                i, j = i-1, j-1
                while n and j >= start:
                    n, j = n-1, j-1
                i, j = i-1, j+1
                while n and i > start:
                    n, i = n-1, i-1
                start += 1
                rows, cols = rows-1, cols-1
            return answer

Log in to reply

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