Simple Java Solution : Reflection + Transpose


  • 0
    S
    public class Solution {
        public void rotate(int[][] matrix) {
            int n = matrix.length, m = matrix[0].length;
            int start = 0, end = n - 1;
            // refelction on y axis
            while (start < end) {
                for (int i = 0; i < m; i ++) {
                    int temp = matrix[start][i];
                    matrix[start][i] = matrix[end][i];
                    matrix[end][i] = temp;
                }
                start++;
                end--;
            }
            
            // tranpose
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < m; j++) {
                    int temp = matrix[i][j];
                    matrix[i][j] = matrix[j][i];
                    matrix[j][i] = temp;
                }
            }
            
        }
    }
    

Log in to reply
 

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