Javascript, solution-sharing, ez to understand


  • 0
    C
    [javascript]
    
    /**
     * setp 1: define a two-dimensional array, solution
     * setp 2: assign solution value
     * setp 3: make matrix = solution
     */
    var rotate = function(matrix) {
        var solution = [];
        var length = matrix.length;
        for(var i=0; i<length; ++i){     // step 1
            solution[i] = [];
        }
        for(i=0; i<length; ++i){         // step 2
            for(var j=0; j<length; ++j){
                solution[j][length-i-1] = matrix[i][j];
            }
        }
        for(i=0; i<length; ++i){         // step 3
            for(j=0; j<length; ++j){
                matrix[i][j] = solution[i][j];
            }
        }
    };

Log in to reply
 

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