Skip to content

Commit ad4304d

Browse files
committed
solve problem Rotate Image
1 parent 905f4e3 commit ad4304d

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ All solutions will be accepted!
289289
|537|[Complex Number Multiplication](https://leetcode-cn.com/problems/complex-number-multiplication/description/)|[java/py/js](./algorithms/ComplexNumberMultiplication)|Medium|
290290
|228|[Summary Ranges](https://leetcode-cn.com/problems/summary-ranges/description/)|[java/py/js](./algorithms/SummaryRanges)|Medium|
291291
|890|[Find And Replace Pattern](https://leetcode-cn.com/problems/find-and-replace-pattern/description/)|[java/py/js](./algorithms/FindAndReplacePattern)|Medium|
292+
|48|[Rotate Image](https://leetcode-cn.com/problems/rotate-image/description/)|[java/py/js](./algorithms/RotateImage)|Medium|
292293

293294
# Database
294295
|#|Title|Solution|Difficulty|

algorithms/RotateImage/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Rotate Image
2+
This problem is easy to solve

algorithms/RotateImage/Solution.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public void rotate(int[][] matrix) {
3+
int n = matrix.length,
4+
i = 0;
5+
6+
while (i < n / 2) {
7+
int x = i,
8+
y = i;
9+
10+
while (y < n - 1 - i) {
11+
matrix[x][y] += matrix[y][n - 1 -x];
12+
matrix[y][n - 1 -x] = matrix[x][y] - matrix[y][n - 1 -x];
13+
matrix[x][y] -= matrix[y][n - 1 -x];
14+
15+
matrix[x][y] += matrix[n - 1 - x][n - 1 - y];
16+
matrix[n - 1 - x][n - 1 - y] = matrix[x][y] - matrix[n - 1 - x][n - 1 - y];
17+
matrix[x][y] -= matrix[n - 1 - x][n - 1 - y];
18+
19+
matrix[x][y] += matrix[n - 1 - y][x];
20+
matrix[n - 1 - y][x] = matrix[x][y] - matrix[n - 1 - y][x];
21+
matrix[x][y] -= matrix[n - 1 - y][x];
22+
23+
y++;
24+
}
25+
i++;
26+
}
27+
}
28+
}

algorithms/RotateImage/solution.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {void} Do not return anything, modify matrix in-place instead.
4+
*/
5+
var rotate = function(matrix) {
6+
let n = matrix.length,
7+
i = 0
8+
9+
while (i < parseInt(n / 2)) {
10+
let x = i,
11+
y = i
12+
13+
while (y < n - 1 - i) {
14+
matrix[x][y] += matrix[y][n - 1 -x]
15+
matrix[y][n - 1 -x] = matrix[x][y] - matrix[y][n - 1 -x]
16+
matrix[x][y] -= matrix[y][n - 1 -x]
17+
18+
matrix[x][y] += matrix[n - 1 - x][n - 1 - y]
19+
matrix[n - 1 - x][n - 1 - y] = matrix[x][y] - matrix[n - 1 - x][n - 1 - y]
20+
matrix[x][y] -= matrix[n - 1 - x][n - 1 - y]
21+
22+
matrix[x][y] += matrix[n - 1 - y][x]
23+
matrix[n - 1 - y][x] = matrix[x][y] - matrix[n - 1 - y][x]
24+
matrix[x][y] -= matrix[n - 1 - y][x]
25+
26+
y++
27+
}
28+
i++
29+
}
30+
};

algorithms/RotateImage/solution.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def rotate(self, matrix):
3+
"""
4+
:type matrix: List[List[int]]
5+
:rtype: void Do not return anything, modify matrix in-place instead.
6+
"""
7+
n = len(matrix)
8+
i = 0
9+
10+
while i < n / 2:
11+
x = i
12+
y = i
13+
14+
while y < n - 1 - i:
15+
matrix[x][y], matrix[y][n - 1 -x] = matrix[y][n - 1 -x], matrix[x][y]
16+
matrix[x][y], matrix[n - 1 - x][n - 1 - y] = matrix[n - 1 - x][n - 1 - y], matrix[x][y]
17+
matrix[x][y], matrix[n - 1 - y][x] = matrix[n - 1 - y][x], matrix[x][y]
18+
y += 1
19+
20+
i += 1

0 commit comments

Comments
 (0)