Sharing JS solution. 128ms


  • 0
    K
    var rotate = function(matrix){
      var n = matrix.length;
      
      for (var i=0; i<n;i++){
        for(var j=0; j<n; j++){
          var tmp = matrix[i][j];
          matrix[i][j] = matrix[j][i];
          matrix[j][i] = tmp;
          if(i==j){
            break;
          }
        }
      }
      
      for (var k=0; k<n; k++){
        matrix[k].reverse();
      }
    
    };

  • 0
    T
    var rotate = function(matrix) {
        if(matrix.length == 0 || matrix.length != matrix[0].length){
           return false;
        }
        var res = JSON.parse(JSON.stringify(matrix));
        n = matrix.length;
        for(i=0;i<n;i++){
            for(j=n-1;j>-1;j--){
                matrix[i][n-j-1] = res[j][i];         
            }
        }
        
    };
    

Log in to reply
 

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