Skip to content

Commit

Permalink
add 048 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 11, 2023
1 parent eeb5747 commit e431fff
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 10 deletions.
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20231211: 39,53
20231211: 39,53,48
20231208: 33
20231206: 19,003
20231205: 23
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
2024-02-04 -> ['39,53']
2024-02-04 -> ['39,53,48']
2024-01-30 -> ['19,003']
2024-01-29 -> ['23']
2024-01-28 -> ['55,45']
2024-01-14 -> ['39,53']
2024-01-14 -> ['39,53,48']
2024-01-09 -> ['19,003']
2024-01-08 -> ['23']
2024-01-07 -> ['55,45']
2024-01-02 -> ['452,406,135']
2024-01-01 -> ['39,53']
2024-01-01 -> ['39,53,48']
2023-12-31 -> ['134']
2023-12-28 -> ['53']
2023-12-27 -> ['19,003']
2023-12-26 -> ['23']
2023-12-25 -> ['55,45']
2023-12-24 -> ['39,53']
2023-12-19 -> ['39,53', '19,003']
2023-12-24 -> ['39,53,48']
2023-12-19 -> ['39,53,48', '19,003']
2023-12-18 -> ['23']
2023-12-17 -> ['55,45']
2023-12-16 -> ['39,53']
2023-12-14 -> ['39,53', '19,003']
2023-12-13 -> ['39,53', '23']
2023-12-12 -> ['39,53', '55,45', '452,406,135']
2023-12-16 -> ['39,53,48']
2023-12-14 -> ['39,53,48', '19,003']
2023-12-13 -> ['39,53,48', '23']
2023-12-12 -> ['39,53,48', '55,45', '452,406,135']
2023-12-11 -> ['19,003']
2023-12-10 -> ['23', '134']
2023-12-09 -> ['19,003', '55,45']
Expand Down
137 changes: 137 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Array/RotateImage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package LeetCodeJava.Array;

//https://leetcode.com/problems/rotate-image/

public class RotateImage {

/**
* Exp 1:
*
* matrix = [[1,2,3],[4,5,6],[7,8,9]]
*
* // Step 1) : mirror ([i, j] -> [j, i])
*
* [
* [1,4,7],
* [2,5,8],
* [3,6,9]
* ]
*
* // Step 2) : reverse ([1,2,3] -> [3,2,1])
*
* [
* [7,4,1],
* [8,5,2],
* [9,6,3]
* ]
*
*/

// V0
// IDEA : ARRAY OP
public void rotate(int[][] matrix){

int len = matrix.length;
int width = matrix[0].length;

// Step 1) : mirror ([i, j] -> [j, i])
for (int i = 0; i < len; i++){
for (int j = i+1; j < width; j++){
int tmp = matrix[i][j];
//matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}

// Step 2) : reverse ([1,2,3] -> [3,2,1])
for (int i = 0; i < len; i++){
_reverse(matrix[i]);
}
}

private void _reverse(int[] input){

int len = input.length;
/**
* Exp 1
*
* [1,2,3,4]
* l r
*
* [4,2,3,1]
* l r
*
*
* Exp 2
*
* [1,2,3,4,5]
* l r
*
* [5,2,3,4,1]
* l r
*
* [5,4,3,2,1]
* l
* r
*/
int l = 0;
int r = len-1;
while(r > l){
int _tmp = input[l];
// i, j = j, i
input[l] = input[r];
input[r] = _tmp;
l += 1;
r -= 1;
}

}

// V1
// IDEA : Rotate Groups of Four Cells
// https://leetcode.com/problems/rotate-image/editorial/
public void rotate_1(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < (n + 1) / 2; i ++) {
for (int j = 0; j < n / 2; j++) {
int temp = matrix[n - 1 - j][i];
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - j - 1];
matrix[n - 1 - i][n - j - 1] = matrix[j][n - 1 -i];
matrix[j][n - 1 - i] = matrix[i][j];
matrix[i][j] = temp;
}
}
}

// V2
// IDEA : Reverse on the Diagonal and then Reverse Left to Right
// https://leetcode.com/problems/rotate-image/editorial/
public void rotate_2(int[][] matrix) {
transpose(matrix);
reflect(matrix);
}

public void transpose(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int tmp = matrix[j][i];
matrix[j][i] = matrix[i][j];
matrix[i][j] = tmp;
}
}
}

public void reflect(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n / 2; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[i][n - j - 1];
matrix[i][n - j - 1] = tmp;
}
}
}

}

0 comments on commit e431fff

Please sign in to comment.