Javascript solution. Works for NxM as well


  • 0
    V

    I just added the rows and col variables to make it work with an NxM matrix as well.

    var rotate = function(matrix) {
        var rows = matrix.length,
            col = matrix[0].length;
    
        for (var i = 0; i < rows; i++){
            for (var j = i + 1; j < col; j++){
                var temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
    
        for (i = 0; i < rows; i++){
            for (j = 0; j < col / 2; j++){
                temp = matrix[i][j];
                matrix[i][j] = matrix[i][col - 1 - j];
                matrix[i][col - 1 - j] = temp;
            }
        }
    };
    

Log in to reply
 

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