Straight method.Java.


  • 0
    J
        //clockwise rotation is like m[i][j]->m[j][n-i]->m[n-i][n-i]->m[n-i][i]->beginging, do counterclockwise rotation need only constant space.
        public void rotate(int[][] a) {
    		int n = a.length;
    		if (n == 0 || n == 1) {
    			return;
    		}
    		for (int i = 0; i < n / 2; i++) { //i+1 = layer of the square
    		    for(int j=i;j<n-(i+1);j++){
    		        int temp = a[i][j];
    				a[i][j] = a[n - 1 - j][i];
    				a[n - 1 - j][i] = a[n - 1 - i][n - 1 - j];
    				a[n - 1 - i][n - 1 - j] = a[j][n - 1 - i];
    				a[j][n - 1 - i] = temp;
    		   }
    		}
        }

Log in to reply
 

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