47. Rotate Image
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
---
public class Solution { public void rotate(int[][] matrix) { if(matrix == null) return; if(matrix.length != matrix[0].length) return; int N = matrix.length - 1; for(int i=0; i<(N+1)/2; i++){ for(int j=i; j<N-i; j++){ int tmp = matrix[i][j]; matrix[i][j] = matrix[N-j][i]; matrix[N-j][i] = matrix[N-i][N-j]; matrix[N-i][N-j] = matrix[j][N-i]; matrix[j][N-i] = tmp; } } } }
浙公网安备 33010602011771号