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 try to follow the path. Follow four numbers instead.

Keep top, bottom, left and right

.....

.....

.....

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

Pedro Grillo Morales

· a month 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