Open jason89521 opened 3 months ago
There is another approach that doesn't require checking the length of the result every time we want to push an element to the result.
We can use an integer to record the direction: 1 means from left to right or from top to bottom, and -1 means from right to left or from bottom to top. We also use two variables, rows
and cols
, to record how many rows or columns are left to handle.
Practice Dates
Description
Link: https://leetcode.com/problems/spiral-matrix/description/
Solution
Record all the boundaries and check whether the length of the result vector is equal to the total number of elements in the matrix.
Performance
Time complexity: