forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateImage.java
More file actions
78 lines (69 loc) · 1.79 KB
/
RotateImage.java
File metadata and controls
78 lines (69 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.leetcode.problems.medium;
import com.geeksforgeeks.array.Rotate2DMatrix;
/**
* @author neeraj on 08/10/19
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class RotateImage {
public static void main(String[] args) {
int[][] matrix = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Rotate2DMatrix.print2DArray(matrix);
rotate(matrix);
Rotate2DMatrix.print2DArray(matrix);
}
/**
* Simple Transpose the matrix
* and just flip it
* 1 2 3
* 4 5 6
* 7 8 9
* <p>
* So transpose of it
* 1 4 7
* 2 5 8
* 3 6 9
* <p>
* // Now look at what we want to achieve is just the flip of above transpose output
* <p>
* 7 4 1
* 8 5 2
* 9 6 3
*
* @param matrix
*/
public static void rotate(int[][] matrix) {
transposeMatrix(matrix);
// Now just flip it
for (int i = 0; i < matrix.length; i++) {
swap1DMatrix(matrix[i]);
}
}
private static void swap1DMatrix(int[] matrix) {
int left = 0;
int right = matrix.length - 1;
while (left < right) {
int temp = matrix[left];
matrix[left] = matrix[right];
matrix[right] = temp;
left++;
right--;
}
}
private static void transposeMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = i; j < matrix[i].length; j++) {
swap(matrix, i, j, j, i);
}
}
}
private static void swap(int[][] matrix, int i, int j, int k, int l) {
int temp = matrix[i][j];
matrix[i][j] = matrix[k][l];
matrix[k][l] = temp;
}
}