The idea below is to get the symmetric matrix first, then reverse each line. The rotate direction decides which way to symmetric, reverse will be the same way.
public class Solution {
public void rotate(int[][] matrix) {
if(matrix == null){
return;
}
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < i; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
int begin = 0;
int end = matrix[0].length - 1;
while(begin < end){
for(int i = 0; i < matrix.length; i++){
int temp = matrix[i][begin];
matrix[i][begin] = matrix[i][end];
matrix[i][end] = temp;
}
begin++;
end--;
}
}
}
没有评论:
发表评论