# Java solution -left top (i,j)-> right top (j,n-1-i)->right bottom(n-1-i,n-1-j)-> left bottom(n-1-j,i)

• ``````   //The key point is :left top (i,j)-> right top (j,n-1-i)->right bottom(n-1-i,n-1-j)-> left bottom(n-1-j,i)
public class Solution {
public void rotate(int[][] matrix) {
int piles = (int)Math.ceil((matrix.length) /2.0);
for(int i= 0;i < piles; i++){
for(int j = i;j<matrix.length -1-i;j++){
int tmp1 = matrix[j][matrix.length -1 -i];
matrix[j][matrix.length -1 - i] = matrix[i][j];
int tmp2 = matrix[matrix.length -1 - i][matrix.length -1 - j];
matrix[matrix.length -1- i][matrix.length -1- j]= tmp1;
int tmp3= matrix[matrix.length -1 - j][i];
matrix[matrix.length -1- j][i] = tmp2;
matrix[i][j] = tmp3;
}
}
}
}``````

• `l1i == length - 1 - i`, `l1j == length - 1 - j`

``````		    i	j	l1j	l1i
x	x	x	x	x	x	0
i  	x	x	A	x	x	x	1
j  	x	x	x	x	B	x	2
l1j	x	D	x	x	x	x	3
l1i	x	x	x	C	x	x	4
x	x	x	x	x	x	5
0	1	2	3	4	5	6

originally    : A B C D
tmp1=B, B=A   : A A C D
tmp2=C, C=tmp1: A A B D
tmp3=D, D=tmp2: A A B C
A=tmp3        : D A B C``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.