Treat image as a series of loops (or squares), and rotate loop by loop


  • 0
    J

    I rotate the image 90 degrees from outer loop all the way to the inner loop. no flipping.

    '''
    public class Solution {
    public void rotate(int[][] matrix) {
    int len = matrix.length;

        for(int i = 0;  i< len/2; i++){
            for(int j = 0; j<len-1-2*i; j++){
                int temp = matrix[i][i+j];
    
                matrix[i][i+j] = matrix[len-1-i-j][i];
                matrix[len-1-i-j][i] = matrix[len-1-i][len-1-i-j];
                matrix[len-1-i][len-1-i-j] = matrix[i+j][len-1-i];
                matrix[i+j][len-1-i] = temp;
            }
        }    
    }
    

    }
    '''


Log in to reply
 

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