-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_solution.js
102 lines (85 loc) · 2.35 KB
/
my_solution.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
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
/**
* @param {number[][]} matrix
* @return {number[]}
*/
const spiralOrder = (matrix) => {
let left = 0, right = matrix[0].length, top = 0, bot = matrix.length, spiralList = [];
while (left < right && top < bot) {
for (let i = left; i < right; i++) {
spiralList.push(matrix[top][i]);
}
top += 1;
if (left >= right || top >= bot) {
console.log("??")
break;
}
for (let i = top; i < bot; i++) {
spiralList.push(matrix[i][right - 1]);
}
right -= 1;
for (let i = right - 1; i >= left; i--) {
spiralList.push(matrix[bot - 1][i]);
}
bot -= 1;
for (let i = bot - 1; i >= top; i--) {
spiralList.push(matrix[i][left]);
}
left += 1;
console.log(`> l:${left}, r:${right}, t:${top}, b:${bot}`)
}
console.log(matrix)
return spiralList;
};
let x = null;
// [1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100,99,98,97,96,95,94,93,92,91,81,71,61,51,41,31,21,11,12,13,14,15,16,17,18,19,29,39,49,59,69,79,89,88,87,86,85,84,83,82,72,62,52,42,32,22,23,24,25,26,27,28,38,48,58,68,78,77,76,75,74,73,63,53,43,33,34,35,36,37,47,57,67,66,65,64,54,44,45,46,56,55]
// x = spiralOrder([
// [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]
// ]);
// [1,2,3,4,5,10,15,20,25,24,23,22,21,16,11,6,7,8,9,14,19,18,17,12,13]
// x = spiralOrder([
// [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]
// ]);
// x = spiralOrder([[1]]);
// [1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10]
// x = spiralOrder([
// [1, 2, 3, 4],
// [5, 6, 7, 8],
// [9, 10, 11, 12],
// [13, 14, 15, 16]
// ]);
// [1,2,3,4,8,12,11,10,9,5,6,7]
x = spiralOrder([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]);
// x = spiralOrder([
// [1, 2, 3, 4]
// ]);
// x = spiralOrder([
// [1],
// [2],
// [3],
// [4]
// ]);
// [1,2,3,6,9,8,7,4,5]
// x = spiralOrder([
// [1, 2, 3],
// [4, 5, 6],
// [7, 8, 9]
// ]);
console.log("> R")
console.log(x)