-
Notifications
You must be signed in to change notification settings - Fork 0
/
59.js
39 lines (36 loc) · 817 Bytes
/
59.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
// walk the spiral
var generateMatrix = function(n) {
let m = [];
for(let i=0;i<n;i++){
m[i]=[];
for(let j=0;j<n;j++)m[i][j]=0;
}
m[-1]=[],m[n]=[];
let count = 1, i = 0, j=0, dir = 0;//0,1,2,3
let di = [1,0,-1,0], dj=[0,1,0,-1];
while(count<=n*n){
while(m[j][i]!==undefined){
if(m[j][i]!==0)break;
m[j][i]=count;
count++;
i+=di[dir];j+=dj[dir];
}
i-=di[dir];j-=dj[dir];
switch(dir){
case 0:
j++;
break;
case 1:
i--;
break;
case 2:
j--;
break;
case 3:
i++;
break;
}
dir = dir+1>3?0:dir+1;
}
return m.slice(0,n);
};