Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7ac271e

Browse files
Adding spiral matrix
1 parent 4a00567 commit 7ac271e

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

‎matrix/directions.md‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# --- Directions
2+
3+
Write a function that accepts an integer N
4+
and returns a NxN spiral matrix.
5+
6+
# --- Examples
7+
matrix(2) should return:
8+
[[1, 2],
9+
[4, 3]]
10+
matrix(3) should return:
11+
[[1, 2, 3],
12+
[8, 9, 4],
13+
[7, 6, 5]]
14+
matrix(4) should return:
15+
[[1, 2, 3, 4],
16+
[12, 13, 14, 5],
17+
[11, 16, 15, 6],
18+
[10, 9, 8, 7]]
19+
20+
21+
# --- Solution
22+
23+
24+
function matrix(n) {
25+
const results = [];
26+
27+
for(let i = 0; i < n; i++) {
28+
results.push([]);
29+
}
30+
31+
let counter = 1;
32+
let startRow = 0;
33+
let endRow = n-1;
34+
let startColumn = 0;
35+
let endColumn = n-1;
36+
37+
while(startColumn <= endColumn && startRow <= endRow) {
38+
// Top Row
39+
for(let i = startColumn; i <= endColumn; i++) {
40+
results[startRow][i] = counter;
41+
counter++
42+
}
43+
44+
startRow++
45+
46+
// Right Column
47+
for(let i = startRow; i <= endRow; i++) {
48+
results[i][endColumn] = counter;
49+
counter++;
50+
}
51+
52+
endColumn--;
53+
54+
for(let i = endColumn; i>=startColumn; i--){
55+
results[endRow][i] = counter;
56+
counter++;
57+
}
58+
59+
endRow--;
60+
61+
for(let i = endRow; i >= startRow; i--){
62+
results[i][startColumn] = counter;
63+
counter++;
64+
}
65+
66+
startColumn++;
67+
}
68+
69+
return results;
70+
}

‎matrix/matrix.js‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// --- Directions
2+
// Write a function that accepts an integer N
3+
// and returns a NxN spiral matrix.
4+
// --- Examples
5+
// matrix(2)
6+
// [[1, 2],
7+
// [4, 3]]
8+
// matrix(3)
9+
// [[1, 2, 3],
10+
// [8, 9, 4],
11+
// [7, 6, 5]]
12+
// matrix(4)
13+
// [[1, 2, 3, 4],
14+
// [12, 13, 14, 5],
15+
// [11, 16, 15, 6],
16+
// [10, 9, 8, 7]]
17+
18+
function matrix(n) {
19+
const results = [];
20+
21+
for(let i = 0; i < n; i++) {
22+
results.push([]);
23+
}
24+
25+
let counter = 1;
26+
let startRow = 0;
27+
let endRow = n-1;
28+
let startColumn = 0;
29+
let endColumn = n-1;
30+
31+
while(startColumn <= endColumn && startRow <= endRow) {
32+
// Top Row
33+
for(let i = startColumn; i <= endColumn; i++) {
34+
results[startRow][i] = counter;
35+
counter++
36+
}
37+
38+
startRow++
39+
40+
// Right Column
41+
for(let i = startRow; i <= endRow; i++) {
42+
results[i][endColumn] = counter;
43+
counter++;
44+
}
45+
46+
endColumn--;
47+
48+
for(let i = endColumn; i>=startColumn; i--){
49+
results[endRow][i] = counter;
50+
counter++;
51+
}
52+
53+
endRow--;
54+
55+
for(let i = endRow; i >= startRow; i--){
56+
results[i][startColumn] = counter;
57+
counter++;
58+
}
59+
60+
startColumn++;
61+
}
62+
63+
return results;
64+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /