Grokking Data Structures & Algorithms for Coding Interviews
Vote

0% completed

Solution: Spiral Matrix

Problem Statement

Given an m x n matrix, return all of its values in spiral order.

Examples

Example 1

  • Input: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  • Expected 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]]
  • Expected Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]

Example 3

  • Input: matrix = [[7]]
  • Expected Output: [7]

Solution

Do not think about it as a path. Think about it as four boundaries closing in.

Keep four numbers: top, bottom, left and

.....

.....

.....

Like the course? Get enrolled and start learning!
Pedro Grillo Morales

Pedro Grillo Morales

· 9 hours ago

My suggestion, simplify the explanation of the source code for the solution as it is convoluted and variables are truly confusing. It is hard to follow.

Reading Progress

0%


Vote for new content