-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-Spiral-Traverse.js
47 lines (41 loc) · 1.17 KB
/
10-Spiral-Traverse.js
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
function spiralTraverse(array) {
// Write your code here.
let result = [];
let minRow = 0;
let minCol = 0;
let maxRow = array.length - 1;
let maxCol = array[0].length - 1;
let total = (maxCol + 1) * (maxRow + 1); // Error for n*m size of matrix
let count = 0;
while (count < total) {
// top wall
for (let i = minRow, j = minCol; j <= maxCol && count < total; j++) {
result.push(array[i][j]);
count++;
}
minRow++;
// left wall
for (let i = minRow, j = maxCol; i <= maxRow && count < total; i++) {
result.push(array[i][j]);
count++;
}
maxCol--;
// bottom wall
for (let i = maxRow, j = maxCol; j >= minCol && count < total; j--) {
result.push(array[i][j]);
count++;
}
maxRow--;
// console.log(maxRow, minCol,minRow, maxRow >= minRow,count,total, count < total)
// right wall
for (let i = maxRow, j = minCol; i >= minRow && count < total; i--) {
console.log(array[i][j]);
result.push(array[i][j]);
count++;
}
minCol++;
}
return result;
}
// Do not edit the line below.
exports.spiralTraverse = spiralTraverse;