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?
1 public class Solution { 2 public void rotate(int[][] matrix) { 3 int len = matrix.length; 4 if(len<=0) return; 5 for(int i=0;i<len-1;i++){ 6 for(int j=0;j<len-i;j++){ 7 swap(matrix,i,j,len-1-j,len-1-i); 8 } 9 } 10 for(int i=0;i<len/2;i++){ 11 for(int j=0;j<len;j++){ 12 swap(matrix,i,j,len-1-i,j); 13 } 14 } 15 } 16 public void swap(int[][]matrix,int x,int y,int x2,int y2){ 17 int temp = matrix[x][y]; 18 matrix[x][y] = matrix[x2][y2]; 19 matrix[x2][y2] = temp; 20 } 21 }