Skip to content

Commit 06ca52f

Browse files
committed
✨ [48] Rotate Image
1 parent 2b57a4d commit 06ca52f

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

48/my_solution.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {void} Do not return anything, modify matrix in-place instead.
4+
*/
5+
const rotate = (matrix) => {
6+
let maxWidth = matrix[0].length;
7+
for (let i = matrix.length - 1; i >= 0; i--) {
8+
// console.log(i)
9+
// console.log(matrix[i])
10+
for (let j = 0; j < maxWidth; j++) {
11+
// console.log(matrix[j][maxWidth + (maxWidth - i)])
12+
// matrix[j][maxWidth + (maxWidth - i)].push(matrix[i][j]);
13+
matrix[j].splice(maxWidth + (maxWidth - i), 0, (matrix[i][j]));
14+
// matrix[j].splice(maxWidth + (maxWidth - i), 0, (matrix[i][j]));
15+
}
16+
17+
if (i === 0) {
18+
console.log("maxWidth")
19+
console.log(maxWidth)
20+
for (let k = 0; k < maxWidth; k++) {
21+
console.log(matrix)
22+
matrix[k].splice(0, maxWidth);
23+
// matrix[j].splice(maxWidth + (maxWidth - i), 0, (matrix[i][j]));
24+
}
25+
}
26+
}
27+
28+
console.log("Result")
29+
console.log(matrix)
30+
};
31+
32+
// rotate([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
33+
rotate([[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]]);

48/solution.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {void} Do not return anything, modify matrix in-place instead.
4+
*/
5+
const rotate = (matrix) => {
6+
let maxWidth = matrix[0].length;
7+
for (let i = matrix.length - 1; i >= 0; i--) {
8+
for (let j = 0; j < maxWidth; j++) {
9+
matrix[j].splice(maxWidth + (maxWidth - i), 0, (matrix[i][j]));
10+
}
11+
12+
if (i === 0) {
13+
for (let k = 0; k < maxWidth; k++) {
14+
matrix[k].splice(0, maxWidth);
15+
}
16+
}
17+
}
18+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- [22. Generate Parentheses](./22/)
2727
- [23. Merge k Sorted Lists](./23/)
2828
- [33. Search in Rotated Sorted Array](./33/)
29+
- [48. Rotate Image](./48/)
2930
- [49. Group Anagrams](./49/)
3031
- [53. Maximum Subarray](./53/)
3132
- [54. Spiral Matrix](./54/)
@@ -118,7 +119,7 @@ Batch create:
118119
NOTE: JS IS HERE
119120
-->
120121
```ssh
121-
chapter=54 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
122+
chapter=48 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
122123
```
123124
> then you can use `x` for quick debug.
124125

0 commit comments

Comments
 (0)