0054: Spiral Matrix

Problem Statement

Given an m x n matrix, return all elements of the matrix in spiral order.

Example 1:

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]

Example 2:

Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]

Constraints:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 10
  • -100 <= matrix[i][j] <= 100

Code Solution

class Solution:
    def spiralOrder(self, matrix):
	    
        nR = len(matrix)
        nC = len(matrix[0])
        
        initR = 0
        initC = 0
        stopR = nR - 1
        stopC = nC - 1
        
        out = []
        count = 0
        while count < nR * nC:
            
            if count < nR * nC:
                for cx in range(initC, stopC + 1, +1):
                    out.append(matrix[initR][cx])
                    count = count + 1
                initR = initR + 1
            if count < nR * nC:
                for rx in range(initR, stopR + 1, +1):
                    out.append(matrix[rx][stopC])
                    count = count + 1
                stopC = stopC - 1
            if count < nR * nC:
                for cx in range(stopC, initC - 1, -1):
                    out.append(matrix[stopR][cx])
                    count = count + 1
                stopR = stopR - 1
            if count < nR * nC:
                for rx in range(stopR, initR - 1, -1):
                    out.append(matrix[rx][initC])
                    count = count + 1
                initC = initC + 1
        
        return out