-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
69 lines (67 loc) · 1.68 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @param {number[][]} matrix
* @return {number[]}
*/
var spiralOrder = function(matrix) {
let result = [];
const rules = {
right:{
next:'bottom',
action(arr){
const first_row = arr.shift();
result = result.concat(first_row);
total_count -= first_row.length;
},
},
bottom:{
next:'left',
action(arr){
arr.forEach((row) => {
result.push(row.pop());
});
total_count -= arr.length;
}
},
left:{
next:'top',
action(arr){
const last_row = arr.pop();
result = result.concat(last_row.reverse());
total_count -= last_row.length;
}
},
top:{
next:'right',
action(arr){
for (let i = arr.length - 1; i >=0; i--) {
result.push(arr[i].shift())
}
total_count -= arr.length;
}
},
};
let total_count = matrix.reduce((res, cur) => cur.length + res, 0);
let current_direction = 'right';
while (total_count > 0) {
rules[current_direction].action(matrix);
current_direction = rules[current_direction].next;
}
return result;
};
const test1 = [
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
];
const test2 = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
];
// console.log(spiralOrder(test2));
module.exports = {
id:'54',
title:'Spiral Matrix',
url:'https://leetcode.com/problems/spiral-matrix/',
difficulty:'medium',
};