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 b099583

Browse files
solves rotate image
1 parent 12861a4 commit b099583

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
| 45 | [Jump Game II](https://leetcode.com/problems/jump-game-ii) | [![Java](assets/java.png)](src/JumpGameII.java) | |
4949
| 46 | [Permutations](https://leetcode.com/problems/permutations) | [![Java](assets/java.png)](src/Permutations.java) | |
5050
| 47 | [Permutations II](https://leetcode.com/problems/permutations-ii) | [![Java](assets/java.png)](src/PermutationsII.java) | |
51+
| 48 | [Rotate Image](https://leetcode.com/problems/rotate-image) | [![Java](assets/java.png)](src/RotateImage.java) | |
5152
| 53 | [Maximum SubArray](https://leetcode.com/problems/maximum-subarray) | [![Java](assets/java.png)](src/MaximumSubArray.java) [![Python](assets/python.png)](python/maximum_sum_subarray.py) | |
5253
| 55 | [Jump Game](https://leetcode.com/problems/jump-game) | [![Java](assets/java.png)](src/JumpGame.java) | |
5354
| 58 | [Length of Last Word](https://leetcode.com/problems/length-of-last-word) | [![Java](assets/java.png)](src/LengthOfLastWord.java) [![Python](assets/python.png)](python/length_of_last_word.py) | |

‎src/RotateImage.java‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// https://leetcode.com/problems/rotate-image
2+
// T: O(n ^ 2)
3+
// S: O(1)
4+
5+
public class RotateImage {
6+
private static final class Point {
7+
private final int row;
8+
private final int column;
9+
10+
private Point(int row, int column) {
11+
this.row = row;
12+
this.column = column;
13+
}
14+
}
15+
16+
public void rotate(int[][] matrix) {
17+
final int frames = (matrix.length + 1) / 2;
18+
for (int frame = 0 ; frame < frames ; frame++) {
19+
rotate90Degrees(matrix, frame);
20+
}
21+
}
22+
23+
private void rotate90Degrees(int[][] matrix, int frame) {
24+
for (int i = 0 ; i < matrix.length - 2 * frame - 1 ; i++) {
25+
Point p1 = getCoordinates(frame, 0, matrix.length, i);
26+
int temp1 = get(matrix, p1), temp2;
27+
for (int k = 0 ; k < 4 ; k++) {
28+
Point p2 = getCoordinates(frame, (k + 1) % 4, matrix.length, i);
29+
temp2 = get(matrix, p2);
30+
set(matrix, p2, temp1);
31+
temp1 = temp2;
32+
}
33+
}
34+
}
35+
36+
private int get(int[][] matrix, Point point) {
37+
return matrix[point.row][point.column];
38+
}
39+
40+
private void set(int[][] matrix, Point point, int value) {
41+
matrix[point.row][point.column] = value;
42+
}
43+
44+
private Point getCoordinates(int frame, int k, int size, int shift) {
45+
return switch (k) {
46+
case 0 -> new Point(frame, frame + shift);
47+
case 1 -> new Point(frame + shift, size - 1 - frame);
48+
case 2 -> new Point(size - 1 - frame, size - 1 - frame - shift);
49+
case 3 -> new Point(size - 1 - frame - shift, frame);
50+
default -> null;
51+
};
52+
}
53+
}

0 commit comments

Comments
(0)

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