Skip to content

Commit 890d76a

Browse files
author
freemanzhang
committed
recursive and iterative implementation
1 parent 1da07e4 commit 890d76a

File tree

1 file changed

+119
-63
lines changed

1 file changed

+119
-63
lines changed

src/recursion/matrix/SpiralMatrix.java

Lines changed: 119 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -20,76 +20,132 @@ Given a matrix of m x n elements (m rows, n columns), return all elements of the
2020
*/
2121
public class SpiralMatrix
2222
{
23-
public List<Integer> spiralOrder( int[][] matrix )
23+
public List<Integer> spiralOrderIterative( int[][] matrix )
2424
{
25-
if ( matrix == null
26-
|| matrix.length == 0
27-
|| matrix[0].length == 0 )
28-
{
29-
return new ArrayList<>();
30-
}
31-
32-
List<Integer> result = new ArrayList<>();
33-
spiralOrder( matrix, 0, 0, matrix.length, matrix[0].length, result );
34-
return result;
25+
List<Integer> result = new ArrayList<Integer>();
26+
if ( matrix == null || matrix.length == 0 )
27+
return result;
28+
int height = matrix.length;
29+
int width = matrix[0].length;
30+
int xStart = 0;
31+
int yStart = 0;
32+
while ( height > 0 && width > 0 )
33+
{
34+
35+
// if one row/column left, no circle can be formed
36+
if ( height == 1 )
37+
{
38+
for ( int i = 0; i < width; i++ )
39+
{
40+
result.add( matrix[xStart][yStart++] );
41+
}
42+
break;
43+
}
44+
else if ( width == 1 )
45+
{
46+
for ( int i = 0; i < height; i++ )
47+
{
48+
result.add( matrix[xStart++][yStart] );
49+
}
50+
break;
51+
}
52+
// below, process a circle
53+
// top - move right
54+
for ( int i = 0; i < width - 1; i++ )
55+
{
56+
result.add( matrix[xStart][yStart++] );
57+
}
58+
// right - move down
59+
for ( int i = 0; i < height - 1; i++ )
60+
{
61+
result.add( matrix[xStart++][yStart] );
62+
}
63+
// bottom - move left
64+
for ( int i = 0; i < width - 1; i++ )
65+
{
66+
result.add( matrix[xStart][yStart--] );
67+
}
68+
// left - move up
69+
for ( int i = 0; i < height - 1; i++ )
70+
{
71+
result.add( matrix[xStart--][yStart] );
72+
}
73+
xStart++;
74+
yStart++;
75+
height = height - 2;
76+
width = width - 2;
77+
}
78+
79+
return result;
3580
}
36-
37-
private void spiralOrder( int[][] matrix, int xCoor, int yCoor, int height, int width, List<Integer> result )
81+
82+
public List<Integer> spiralOrderRecursive( int[][] matrix )
3883
{
39-
// handle edge cases
40-
if ( height <= 0 || width <= 0 )
41-
{
42-
return;
43-
}
44-
if ( height == 1 && width == 1 )
45-
{
46-
result.add( matrix[xCoor][yCoor] );
47-
return;
48-
}
49-
50-
// handle current level
51-
for ( int i = 0; i < width - 1; i++ )
52-
{
53-
result.add( matrix[xCoor][yCoor++] );
54-
}
55-
for ( int i = 0; i < height - 1; i++ )
56-
{
57-
result.add( matrix[xCoor++][yCoor] );
58-
}
59-
60-
if ( height > 1 )
61-
{
62-
for ( int i = 0; i < width - 1; i++ )
63-
{
64-
result.add( matrix[xCoor][yCoor--] );
65-
}
66-
}
67-
68-
if ( width > 1 )
69-
{
70-
for ( int i = 0; i < height - 1; i++ )
71-
{
72-
result.add( matrix[xCoor--][yCoor] );
73-
}
74-
}
75-
76-
// recurse for the next level
77-
if ( height == 1 || width == 1 )
78-
{
79-
spiralOrder( matrix, xCoor, yCoor, 1, 1, result );
80-
}
81-
else
82-
{
83-
spiralOrder( matrix, xCoor + 1, yCoor + 1, height - 2, width - 2, result );
84-
}
84+
if ( matrix == null || matrix.length == 0 )
85+
{
86+
return new ArrayList<Integer>();
87+
}
88+
return spiralOrder( matrix, 0, 0, matrix.length, matrix[0].length );
8589
}
8690

91+
private List<Integer> spiralOrder( int[][] matrix, int xStart, int yStart, int height, int width )
92+
{
93+
List<Integer> result = new ArrayList<Integer>();
94+
if ( height <= 0 || width <= 0 )
95+
{
96+
return result;
97+
}
98+
// only one element left
99+
if ( height == 1 && width == 1 )
100+
{
101+
result.add( matrix[xStart][yStart] );
102+
return result;
103+
}
104+
105+
// top - move right
106+
for ( int i = 0; i < width - 1; i++ )
107+
{
108+
result.add( matrix[xStart][yStart++] );
109+
}
110+
// right - move down
111+
for ( int i = 0; i < height - 1; i++ )
112+
{
113+
result.add( matrix[xStart++][yStart] );
114+
}
115+
// bottom - move left
116+
if ( width > 1 )
117+
{
118+
for ( int i = 0; i < width - 1; i++ )
119+
{
120+
result.add( matrix[xStart][yStart--] );
121+
}
122+
}
123+
// left - move up
124+
if ( height > 1 )
125+
{
126+
for ( int i = 0; i < height - 1; i++ )
127+
{
128+
result.add( matrix[xStart--][yStart] );
129+
}
130+
}
131+
132+
if ( height == 1 || width == 1 )
133+
{
134+
result.addAll( spiralOrder( matrix, xStart, yStart, 1, 1 ) );
135+
}
136+
else
137+
{
138+
result.addAll( spiralOrder( matrix, xStart + 1, yStart + 1, height - 2, width - 2 ) );
139+
}
140+
return result;
141+
}
142+
87143
@Test
88144
public void test()
89145
{
90-
System.out.println( spiralOrder( new int[][]{ { 1 } } ) );
91-
System.out.println( spiralOrder( new int[][]{ { 1, 2, 3, 4 } } ) );
92-
System.out.println( spiralOrder( new int[][]{ { 1, 2, 3}, {4, 5, 6}, {7, 8, 9} } ) );
93-
System.out.println( spiralOrder( new int[][]{ { 1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} } ) );
146+
// System.out.println( spiralOrder( new int[][]{ { 1 } } ) );
147+
// System.out.println( spiralOrder( new int[][]{ { 1, 2, 3, 4 } } ) );
148+
// System.out.println( spiralOrder( new int[][]{ { 1, 2, 3}, {4, 5, 6}, {7, 8, 9} } ) );
149+
// System.out.println( spiralOrder( new int[][]{ { 1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} } ) );
94150
}
95151
}

0 commit comments

Comments
 (0)