Matrix manipulation involves, matrix traversal and manipulation based on matrix properties
Example: Rotate Image
class Solution {
public void rotate(int[][] matrix) {
//replacing rows with columns
int m = matrix.length;
int n = matrix[0].length;
for(int i = 0;i<m;i++){
for(int j = 0;j<i;j++){
swap(i,j,m,n,matrix);
}
}
//swapping the columns
int p = 0; int q = n-1;
while(p<q){
for(int i =0;i<m;i++){
int temp = matrix[i][p];
matrix[i][p] = matrix[i][q];
matrix[i][q] = temp;
}
p++;
q--;
}
}
public void swap(int i, int j, int m ,int n, int matrix[][]){
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
Top comments (0)