-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralOrderArray.cs
More file actions
108 lines (93 loc) · 3.04 KB
/
SpiralOrderArray.cs
File metadata and controls
108 lines (93 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms.Problem.Arrays
{
[TestClass]
public class SpiralOrderArray
{
public ArrayList PrintSpiralOrderArray(int[,] inputArray)
{
/// Input:
/// [
/// {1,2,3},
/// {4,5,6},
/// {7,8,9}
/// ]
/// Output: 1,2,3,6,9,8,7,4,5
int row = 0; int col = 0;
ArrayList output = new ArrayList();
int rowLength = inputArray.GetLength(0);
int colLength = inputArray.GetLength(1);
while( rowLength > 0 && colLength > 0)
{
// row length = 1
if (rowLength == 1)
{
for(int i=0; i < colLength -1; i++)
{
output.Add(inputArray[row, col++]);
}
break;
}
// col length = 1
else if (colLength == 1)
{
for (int i = 0; i < rowLength - 1; i++)
{
output.Add(inputArray[row++, col]);
}
break;
}
// move left to right
for (int i=0; i< colLength-1; i++)
{
output.Add(inputArray[row, col]);
col = col + 1;
}
// move right to down
for(int i=0;i < rowLength -1; i++)
{
output.Add(inputArray[row, col]);
row = row + 1;
}
// move right to left
for(int i=0; i< colLength - 1; i++)
{
output.Add(inputArray[row, col]);
col = col - 1;
}
// move from down to top
for(int i = 0; i < rowLength -1; i++)
{
output.Add(inputArray[row, col]);
row = row - 1;
}
rowLength = rowLength - 2;
colLength = colLength - 2;
row = row + 1;
col = col + 1;
}
return output;
}
[TestMethod]
public void TestPrintSpiralArray()
{
int[,] inputArray = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 },
{ 10,11,12}
};
ArrayList expectedArray = new ArrayList{ 1,2,3,6,9,12,11,10,7,4,5,8 };
ArrayList output = this.PrintSpiralOrderArray(inputArray);
for (int i = 0; i < expectedArray.Count; i++)
{
Assert.AreEqual(expectedArray[i], output[i]);
}
}
}
}