48

faster than 100%

/**
 * @param {number[][]} matrix
 * @return {void} Do not return anything, modify matrix in-place instead.
 */
var rotate = function(matrix) {
    var n = matrix.length
    for(var i = 0; i < n/2; i++){//多少层旋转
        for(var j = i; j < n - i - 1; j++){//每层旋转的起始与结束索引
            var temp = matrix[i][j]
            matrix[i][j] = matrix[n - j - 1][i]
            matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]
            matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]
            matrix[j][n - i - 1] = temp
        }
    }
    return matrix
};

你可能感兴趣的:(48)