-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path54SpiralArray.cs
148 lines (125 loc) · 4.2 KB
/
54SpiralArray.cs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpiralArray
{
class Program
{
static void Main(string[] args)
{
int dim = 5;
int[][] matrix = new int[dim][];
for (int i = 0; i < dim; i++)
matrix[i] = new int[dim];
for (int i = 0; i < dim; i++)
for (int j = 0; j < dim; j++)
{
matrix[i][j] = i * dim + j;
}
ArrayList list = spiralOrder_2(matrix);
// test case
int[,] matrix2 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
IList<int> list2 = SpiralOrder(matrix2);
}
/**
* latest update:
* Leetcode: Spiral Array
*/
public static IList<int> SpiralOrder(int[,] matrix)
{
IList<int> list = new List<int>();
int len1 = matrix.GetLength(0);
int len2 = matrix.GetLength(1);
int[][] matrix_input = new int[len1][];
for (int i = 0; i < len1;i++ )
matrix_input[i] = new int[len2];
for (int i = 0; i < len1; i++)
for (int j = 0; j < len2; j++)
{
matrix_input[i][j] = matrix[i,j];
}
ArrayList al = spiralOrder_2(matrix_input);
foreach(object s in al)
{
list.Add((int)s);
}
return list;
}
public static ArrayList spiralOrder_2(int[][] matrix)
{
if (matrix == null || matrix.Length == 0 || matrix[0].Length == 0)
return new ArrayList();
return spiralOrder_3(matrix, 0, 0, matrix.Length, matrix[0].Length);
}
/**
* Latest update: June 12, 2015
* Leetcode: spiral array
* http://gongxuns.blogspot.ca/2012/12/leetcode-spiral-matrix.html
* Test case:
* 1. empty array
* 2. one element: 1 -
* 2B. one row : 1 2 --
* 3. one column : 1 |
* 2 |
* 4. more than 1 row,
* or more than 1 column
* 1 2
* 3 4
* output: 1 2 4 3
* 5. 3 rows, 3 columns
* 1 2 3
* 4 5 6
* 7 8 9
* output: 1 2 3 6 9 8 7 4 5
*
*/
public static ArrayList spiralOrder_3(int [][] matrix, int x, int y, int m, int n){
ArrayList res = new ArrayList();
// test case: empty array
if(m<=0||n<=0) return res;
// test case 2: one row and one column
if(m==1&&n==1) {
res.Add(matrix[x][y]);
return res;
}
// row, from left to right
for(int i=0;i<n-1;i++){
res.Add(matrix[x][y++]);
}
// column, from top to down
for(int i=0;i<m-1;i++){
res.Add(matrix[x++][y]);
}
// conditional: second row, from right to left
if(m>1){
for(int i=0;i<n-1;i++){
res.Add(matrix[x][y--]);
}
}
// conditional: second column, from bottom to top
if(n>1){
for(int i=0;i<m-1;i++){
res.Add(matrix[x--][y]);
}
}
// test case: one row, 1 2 3, where 1 2 is procesed above and 3 is left for next round
// one column:
if (m == 1 || n == 1)
{
ArrayList l = spiralOrder_3(matrix, x, y, 1, 1);
foreach(object val in l)
res.Add((int)val);
}
else
{
ArrayList l = spiralOrder_3(matrix, x + 1, y + 1, m - 2, n - 2);
foreach(object val in l)
res.Add((int)val);
}
return res;
}
}
}