Skip to content

Instantly share code, notes, and snippets.

@wushbin
Created February 16, 2020 02:04
Show Gist options
  • Save wushbin/2c3e83027cfc0c756466755f1f940f98 to your computer and use it in GitHub Desktop.
Save wushbin/2c3e83027cfc0c756466755f1f940f98 to your computer and use it in GitHub Desktop.
class Solution {
public void rotate(int[][] matrix) {
trans(matrix);
flip(matrix);
}
private void trans(int[][] matrix) {
int row = matrix.length;
int col = matrix[0].length;
for (int i = 0; i < row; i++) {
for (int j = i + 1; j < col; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
}
private void flip(int[][] matrix) {
int row = matrix.length;
int col = matrix[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col / 2; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[i][ col - 1 - j];
matrix[i][col - 1 - j] = tmp;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment