# Code Kata: implementation of spiral matrix javascript

 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9

As shown in the figure, it is a 5 * 5 spiral matrix

My thoughts are as follows:

Step 1: split the layer array

The matrix is divided into N consecutive natural arrays according to the number of layers. If the width of each layer is n, then there are 4(n-1) numbers in each layer, and when n=1, the number is 1

Split number code

``` 1 function splitNumbers(n,m){ /*Divide the total number into N arrays according to each layer*/
2
3     var arr = [];
4
5     if(n == 1){
6
7         arr[0] = 1 + m;
8
9     }
10     else{
11
12         for(var i=0;i<4*n-4;i++){
13
14             arr[i] = i + 1 + m;
15
16         }
17     }
18
19     return arr;
20 }```

Loop call, n minus 2 each time

``` 1  var m = 0;
2
3     while(n > 0){
4
5         layerArray[count] = splitNumbers(n,m);
6
7         n = n - 2;
8
9         m = layerArray[count][layerArray[count].length - 1];
10
11         count++;
12     }```

Part 2: assemble it into an array of hourglass

Take the first to N and 2n-1 to 3n-2 of the split array, and assemble them to the two ends of the "hourglass" array, where the array at the bottom of the "hourglass" needs to be arranged in reverse order

Partial code

``` 1  for(var i=0;i<totalLayers;i++){/*Assemble into funnel array*/
2
3         var index = i >= layerArray.length ? totalLayers - i - 1 : i;
4
5         var cloneArray = layerArray[index].concat();
6
7         if(i < totalLayers / 2){
8
9             spiralArray[i] = cloneArray.splice(0,totalLayers - index * 2);
10         }
11         else{
12
13             spiralArray[i] = cloneArray.splice(2 * (totalLayers - 2 * i) - 2,totalLayers - index * 2);
14
15             spiralArray[i] = spiralArray[i].reverse();
16         }
17     }```

Part 3: fill the hourglass array into a complete matrix

The "hourglass" array, the first and the last n layers do not need to be filled. Starting from the second layer, fill the n+m bits of each "layer" array and the layers of 4(n-1)-m, m-bit "hourglass" array to both ends successively

Partial code

``` 1     for(var i=0;i<spiralArray.length;i++){/*Filling funnel*/
2
3         var index = i >= layerArray.length ? totalLayers - i - 1 : i;
4
5         for(var j=0;j<index;j++){
6
7             var cloneArray = layerArray[j].concat();
8
9             spiralArray[i].splice(j,0,cloneArray[cloneArray.length - i + j]);
10
11             spiralArray[i].splice(spiralArray[i].length - j,0,cloneArray[totalLayers - 2 * j + i - j - 1]);
12
13         }
14     }```

``` 1 function splitNumbers(n,m){ /*Divide the total number into N arrays according to each layer*/
2
3     var arr = [];
4
5     if(n == 1){
6
7         arr[0] = 1 + m;
8
9     }
10     else{
11
12         for(var i=0;i<4*n-4;i++){
13
14             arr[i] = i + 1 + m;
15
16         }
17     }
18
19     return arr;
20 }
21
22 function spiralNumbers(n){
23
24     var layerArray = [];
25
26     var spiralArray = [];
27
28     var totalLayers = n;
29
30     var count = 0;
31
32     var m = 0;
33
34     while(n > 0){
35
36         layerArray[count] = splitNumbers(n,m);
37
38         n = n - 2;
39
40         m = layerArray[count][layerArray[count].length - 1];
41
42         count++;
43     }
44
45     for(var i=0;i<totalLayers;i++){/*Assemble into funnel array*/
46
47         var index = i >= layerArray.length ? totalLayers - i - 1 : i;
48
49         var cloneArray = layerArray[index].concat();
50
51         if(i < totalLayers / 2){
52
53             spiralArray[i] = cloneArray.splice(0,totalLayers - index * 2);
54         }
55         else{
56
57             spiralArray[i] = cloneArray.splice(2 * (totalLayers - 2 * i) - 2,totalLayers - index * 2);
58
59             spiralArray[i] = spiralArray[i].reverse();
60         }
61     }
62
63     for(var i=0;i<spiralArray.length;i++){/*Filling funnel*/
64
65         var index = i >= layerArray.length ? totalLayers - i - 1 : i;
66
67         for(var j=0;j<index;j++){
68
69             var cloneArray = layerArray[j].concat();
70
71             spiralArray[i].splice(j,0,cloneArray[cloneArray.length - i + j]);
72
73             spiralArray[i].splice(spiralArray[i].length - j,0,cloneArray[totalLayers - 2 * j + i - j - 1]);
74
75         }
76     }
77
78     return spiralArray;
79 }```

Tags: Javascript github

Posted on Wed, 06 May 2020 10:48:31 -0400 by lastcraft