Skip to content

Commit 32c7798

Browse files
committed
Added solution for Leetcode 0048 Rotate Image
1 parent c489a66 commit 32c7798

File tree

6 files changed

+110
-3
lines changed

6 files changed

+110
-3
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
### In-Place Transpose and Reverse Approach
2+
Transpose the matrix: Convert rows to columns.
3+
Reverse each row: Swap the elements horizontally.
4+
5+
Time Complexity: O(n{2})
6+
Space Complexity: O(1)
7+
8+
### In-Place Rotation by Layer
9+
Approach: Rotate the matrix layer by layer.
10+
11+
Important:
12+
The layer-by-layer rotation approach specifically works for 𝑛×𝑛 square matrices. It relies on the symmetry of a square matrix to perform the rotation. For a rectangular matrix (where the number of rows and columns are not equal), this method is not applicable because it will lead to index out-of-bound errors and incorrect rotations.
13+
14+
Steps:
15+
Iterate over each layer of the matrix from the outermost to the innermost.
16+
For each layer, rotate the elements in groups of four.
17+
18+
Time Complexity: O(N{2})
19+
20+
Space Complexity: O(1)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.Arrays;
2+
3+
public class InPlaceRotationByLayer {
4+
public static void main(String[] args) {
5+
int[][] nums = {
6+
{1,2,3},
7+
{4,5,6},
8+
{7,8,9}
9+
};
10+
11+
int left = 0;
12+
int right = nums.length -1;
13+
14+
while (left < right) {
15+
for(int i = 0; i < (right - left); i++) {
16+
int top = left;
17+
int bottom = right;
18+
// save top left
19+
int topLeft = nums[top][left + i];
20+
21+
// move bottom left to top left
22+
nums[top][left + i] = nums[bottom - i][left];
23+
24+
// move top bottom right to bottom left
25+
nums[bottom - i ][left] = nums[bottom][right -i];
26+
27+
// move top right to bottom right
28+
nums[bottom][right - i] = nums[top + i][right];
29+
30+
// move top left to top right
31+
nums[top + i][right] = topLeft;
32+
}
33+
left++;
34+
right--;
35+
}
36+
System.out.println(Arrays.deepToString(nums));
37+
}
38+
}

problems/01. Arrays/Part 02/01. Leetcode Medium 0048. Rotate Image/java/Solution.java

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.Arrays;
2+
public class TransposeAndReverse {
3+
public static void main(String[] args) {
4+
int[][] nums = {
5+
{1,2,3},
6+
{4,5,6},
7+
{7,8,9}
8+
};
9+
10+
int rowLength = nums.length;
11+
int colLength = nums[0].length;
12+
13+
// Transpose the multi-dimensional -> Transpose means simply change row to column and column to row
14+
/*{
15+
{1,4,7},
16+
{2,5,8},
17+
{3,6,9}
18+
}
19+
*/
20+
for(int i = 0; i < rowLength; i++){
21+
for(int j = i+1; j < colLength; j++){
22+
int temp = nums[i][j];
23+
nums[i][j] = nums[j][i];
24+
nums[j][i] = temp;
25+
}
26+
}
27+
28+
for(int row = 0; row < rowLength; row++){
29+
reverse(nums[row]);
30+
}
31+
32+
System.out.println(Arrays.deepToString(nums));
33+
}
34+
35+
private static void reverse(int nums[]){
36+
int left = 0;
37+
int right = nums.length-1;
38+
39+
while(left < right){
40+
int temp = nums[left];
41+
nums[left] = nums[right];
42+
nums[right] = temp;
43+
left++;
44+
right--;
45+
}
46+
}
47+
}

problems/01. Arrays/Part 02/04. Leetcode Medium 0287. Find the Duplicate Number/java/Solution.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
14
class Solution {
25
public int findDuplicate(int[] nums) {
36

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
public class Solution {
2+
3+
24

35
}

0 commit comments

Comments
 (0)