图像旋转
涉及知识:数组
解析:
首先沿左下——右上对角线进行翻转,在上下进行翻转。
代码:
/*
* @lc app=leetcode.cn id=48 lang=java
*
* [48] 旋转图像
*/
class Solution {
public void rotate(int[][] matrix) {
if(matrix == null || matrix.length == 0){
return;
}
int len = matrix.length - 1;
int width = len;
int x, y, tmp;
//将矩阵沿左下---右上对角线翻转
for(int i = 0; i < len; i++){
for(int j = 0; j < width; j++){
x = len - j;
y = len - i;
tmp = matrix[x][y];
matrix[x][y] = matrix[i][j];
matrix[i][j] = tmp;
}
width--;
}
//将矩阵进行上下翻转
len = matrix.length / 2;
int hei = matrix.length - 1;
for(int i = 0; i < len; ++i){
for(int j = 0; j < matrix.length; ++j){
tmp = matrix[hei - i][j];
matrix[hei - i][j] = matrix[i][j];
matrix[i][j] = tmp;
}
}
}
}