Skip to content

Commit ef4d2bf

Browse files
committed
Add solution to 1.7 rotateMatrix
1 parent f533992 commit ef4d2bf

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*Given an image represented by an N*N matrix where
2+
each pixel in the image is 4 bytes, write a method
3+
to rotate the image by 90 degrees. Can you do this
4+
in place?*/
5+
6+
const rotateImage = (nestedArr) => {
7+
if (nestedArr.length === 0) {
8+
return [];
9+
}
10+
var rotatedArr = [];
11+
var numOfRows = nestedArr[0].length;
12+
for (let x = 0; x < numOfRows; x++) {
13+
let newRow = [];
14+
for (let i = nestedArr.length - 1; i >= 0; i--) {
15+
newRow.push(nestedArr[i][x]);
16+
}
17+
rotatedArr.push(newRow);
18+
}
19+
return rotatedArr;
20+
}
21+
22+
// TESTS:
23+
console.log(JSON.stringify(rotateImage([[1,2,3],[4,5,6]])) === JSON.stringify([[4,1],[5,2],[6,3]]));
24+
console.log(JSON.stringify(rotateImage([])) === JSON.stringify([]));
25+
26+
// input: array of array
27+
// output: array of array rotated
28+
// constraits: Do it in place
29+
// edge cases: rotate clockwise? is this represented by an array of arrays of numbers?

0 commit comments

Comments
 (0)