<jupyter_start><jupyter_text>Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.Example 1:Input:[[ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]Output: [1,2,3,6,9,8,7,4,5]Example 2:Input:[[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]<jupyter_code>class Solution:
    def spiralOrder(self, matrix): # 100%
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        if matrix == []:
            return []
        output = []
        m = len(matrix)
        n = len(matrix[0])
        l = 0 # col左極限
        u = 1 # row上極限
        r = n - 1 # col右極限
        d = m - 1 # row下極限
        i = j = 0 #初始 axis index
        output.append(matrix[i][j])
        while True:
            while j < r:
                j += 1
                output.append(matrix[i][j])
            r -= 1
            while i < d:
                i += 1
                output.append(matrix[i][j])
            d -= 1
            while j > l:
                j -= 1
                output.append(matrix[i][j])
            l += 1
            while i > u:
                i -= 1
                output.append(matrix[i][j])          
            u += 1
            if r < l and d < u:
                break
        
        return output[:m*n]
matrix = [[1, 2, 3, 4],
          [5, 6, 7, 8],
          [9,10,11,12]]
ans = Solution()
ans.spiralOrder(matrix)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1544771018455229, 'fraction_numerical': 0.056049213943950786, 'mean_word_length': 1.3312101910828025, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '543804', 'license': 'MIT', 'n_tokens_mistral': 576, 'n_tokens_neox': 512, 'n_words': 138, 'path': '054. Spiral Matrix.ipynb', 'repo_name': 'sppool/LeetCode', 'size': '1463'}