Skip to content

Commit f9d8ecf

Browse files
committed
Add solution #48
1 parent 0e2d804 commit f9d8ecf

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
42|[Trapping Rain Water](./0042-trapping-rain-water.js)|Hard|
4444
43|[Multiply Strings](./0043-multiply-strings.js)|Medium|
4545
46|[Permutations](./0046-permutations.js)|Medium|
46+
48|[Rotate Image](./0048-rotate-image.js)|Medium|
4647
49|[Group Anagrams](./0049-group-anagrams.js)|Medium|
4748
50|[Pow(x, n)](./0050-powx-n.js)|Medium|
4849
53|[Maximum Subarray](./0053-maximum-subarray.js)|Easy|

Diff for: solutions/0048-rotate-image.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 48. Rotate Image
3+
* https://leetcode.com/problems/rotate-image/
4+
* Difficulty: Medium
5+
*
6+
* You are given an n x n 2D matrix representing an image, rotate the image by
7+
* 90 degrees (clockwise).
8+
*
9+
* You have to rotate the image in-place, which means you have to modify the input
10+
* 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
11+
*/
12+
13+
/**
14+
* @param {number[][]} matrix
15+
* @return {void} Do not return anything, modify matrix in-place instead.
16+
*/
17+
var rotate = function(matrix) {
18+
matrix = matrix.reverse();
19+
20+
for (let i = 0; i < matrix.length; i++) {
21+
for (let j = 0; j < i; j++) {
22+
[matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
23+
}
24+
}
25+
26+
return matrix;
27+
};

0 commit comments

Comments
 (0)