|
1 |
| -import java.util.*; |
| 1 | +/* |
| 2 | + Write a function that takes in an n x m two-dimensional array (that can be square-shaped when n == m) and |
| 3 | + returns a one-dimensional array of all the array's elements in spiral order. |
2 | 4 |
|
3 |
| -/** |
4 |
| - * |
| 5 | + Spiral order starts at the top left corner of the two-dimensional array, goes to the right, and proceeds |
| 6 | + in a spiral pattern all the way until every element has been visited. |
5 | 7 |
|
6 |
| - Write a function that takes in an n x m two-dimensional array (that can be square-shaped when n == m) and returns a one-dimensional array of all the array's elements in spiral order. |
| 8 | + Explanation: |
7 | 9 |
|
8 |
| - Spiral order starts at the top left corner of the two-dimensional array, goes to the right, and proceeds in a spiral pattern all the way until every element has been visited. |
9 |
| - Sample Input |
| 10 | + The SpiralTraverse function takes a 2D integer array array and returns a 1D integer slice that contains the |
| 11 | + elements of array traversed in a spiral order, starting from the top-left corner and moving clockwise. |
10 | 12 |
|
11 |
| - array = [ |
12 |
| - [1, 2, 3, 4], |
13 |
| - [12, 13, 14, 5], |
14 |
| - [11, 16, 15, 6], |
15 |
| - [10, 9, 8, 7], |
16 |
| - ] |
| 13 | + The function first initializes an empty slice result to hold the elements of the spiral traversal. |
| 14 | + If the input array is empty, the function immediately returns the empty result. |
17 | 15 |
|
18 |
| - Sample Output |
| 16 | + Next, the function initializes variables startRow, endRow, startCol, and endCol to keep track of the |
| 17 | + boundaries of the matrix. These variables will be updated as the function traverses the matrix. |
19 | 18 |
|
20 |
| - [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] |
| 19 | + The function then enters a loop that traverses the matrix in a spiral order. The loop continues |
| 20 | + as long as startRow <= endRow and startCol <= endCol, which means that there are still elements |
| 21 | + in the matrix to be traversed. |
21 | 22 |
|
| 23 | + The first step in the loop is to traverse the top row of the matrix from left to right, and append |
| 24 | + each element to the result slice. The next step is to traverse the rightmost column of the matrix from top to bottom, |
| 25 | + and append each element to the result slice. If there is more than one row in the matrix, the function then traverses |
| 26 | + the bottom row of the matrix from right to left, and appends each element to the result slice. If there is only one row left, |
| 27 | + the loop is broken to avoid duplicating the elements. Finally, if there is more than one column in the matrix, |
| 28 | + the function traverses the left |
22 | 29 |
|
23 |
| - */ |
| 30 | + O(n) time | O(n) space - where n is the total number of elements in the array |
| 31 | +*/ |
| 32 | +import java.util.*; |
24 | 33 | public class SpiralTraverse {
|
25 | 34 |
|
26 | 35 | public static void main(String[] args) {
|
|
0 commit comments