```
public static void rotate(int[][] matrix) {
int n = matrix.length;
int tmp = 0;
//reverse by left<->right
for (int i = 0; i < n; i ++) {
for (int j = 0; j < n / 2; j ++) {
tmp = matrix[i][j];
matrix[i][j] = matrix[i][n - 1 - j];
matrix[i][n - 1 - j] = tmp;
}
}
//reverse by lefttop<->rightbottom
for (int i = 0; i < n; i ++) {
for (int j = 0; j < n - i; j ++) {
tmp = matrix[i][j];
matrix[i][j] = matrix[n - 1 - j][n - 1 - i];
matrix[n - 1 - j][n - 1 - i] = tmp;
}
}
}
```