findSpiral
to traverse a 2D matrix of int
s in a clockwise spiral order and append the elements to an output ArrayList
if Integer
s.Example:
Input Matrix :
{1, 2, 3}
{4, 5, 6}
{7, 8, 9}
Output ArrayList:[1, 2, 3, 6, 9, 8, 7, 4, 5]
int m = arr.length, n = arr[0].length;
for
loops to traverse the 2D Array.while(m>0 && n>0)...
and decrement m
and n
inside the loop.
public static ArrayList<Integer> findSpiral(int[][] arr) { }
C
Java
Python