-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspiral.js
42 lines (28 loc) · 919 Bytes
/
spiral.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
(function(){
function sprial(matrix,rows,cols){
left=0;right =cols-1;
let top =0, bottom =rows-1;
// while(left <=right &&top<=bottom){
for(let i =left;i<right;i++){
console.log(matrix[top][i]);
}
top++
for(let i =top;i<bottom;i++){
console.log(matrix[i][right]);
}
right--;
if(top<bottom)
for(let i =top;i>left;i--){
console.log(matrix[bottom][i]);
}
bottom--;
}
if(left<right){
for(let i =bottom;i>top;i--){
console.log(matrix[i][left]);
}
}
left++
let matrix=[[1,2,3,4],[5,6,7,8],[9,10,11,12]]
console.log(sprial(matrix 3,4))
})();