Skip to content

Commit afe51bb

Browse files
authored
Merge pull request #1063 from akgmage/dev
Dev
2 parents d73d732 + b28250c commit afe51bb

File tree

2 files changed

+72
-15
lines changed

2 files changed

+72
-15
lines changed

2D Arrays (Matrix)/spiral_traverse.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
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.
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.
7+
8+
Explanation:
9+
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.
12+
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.
15+
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.
18+
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.
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
29+
30+
O(n) time | O(n) space - where n is the total number of elements in the array
31+
*/
132
package main
233

34+
import "fmt"
35+
336
func SpiralTraverse(array [][]int) []int {
437
// Initialize an empty slice to hold the result
538
result := []int{}
@@ -46,3 +79,18 @@ func SpiralTraverse(array [][]int) []int {
4679
// Return the result slice
4780
return result
4881
}
82+
83+
func main() {
84+
// Example 2D array
85+
array := [][]int{
86+
{1, 2, 3, 4},
87+
{10, 11, 12, 5},
88+
{9, 8, 7, 6},
89+
}
90+
91+
// Call SpiralTraverse function on array
92+
result := SpiralTraverse(array)
93+
94+
// Print the result to console
95+
fmt.Println(result)
96+
}

2D Arrays (Matrix)/spiral_traverse.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
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.
24
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.
57
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:
79
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.
1012
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.
1715
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.
1918
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.
2122
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
2229
23-
*/
30+
O(n) time | O(n) space - where n is the total number of elements in the array
31+
*/
32+
import java.util.*;
2433
public class SpiralTraverse {
2534

2635
public static void main(String[] args) {

0 commit comments

Comments
 (0)