leetcode简单(矩阵):[566, 766, 832, 867, 999, 1030, 1261, 1275, 1337, 1351]

566. 重塑矩阵

var matrixReshape = function(mat, r, c) {
    if (mat.length * mat[0].length != r * c) {
        return mat;
    }
    var newArr = mat.flat(Infinity);
    var result = [];
    var k = 0;
    for (var i = 0; i < newArr.length; i++) {
        if (!result[k]) {
            result[k] = [];
        }
        result[k].push(newArr[i]);
        if ((i + 1) % c == 0) {
            k++;
        }
    }
    return result;
};

766. 托普利茨矩阵

var isToeplitzMatrix = function(matrix) {
    for (let i = 0; i < matrix.length; i++) {
        for (let j = 0; j < matrix[0].length; j++) {
            if (
                matrix[i + 1] &&
                matrix[i + 1][j + 1] != undefined &&
                matrix[i][j] != matrix[i + 1][j + 1]
            ) {
                return false;
            }
        }
    }
    return true;
}

832. 翻转图像

var flipAndInvertImage = function(image) {
    var result = [];
    for (let i = 0; i < image.length; i++) {
        for (let j = image[0].length - 1; j >= 0; j--) {
            let tmp = 1 - image[i][j];
            if (!result[i]) {
                result[i] = [];
            }
            result[i].push(tmp);
        }
    }
    return result;
};

867. 转置矩阵

var transpose = function(A) {
    // return A[0].map((v,i) =>A.map((a,j) =>a[i]))
    var res = [];
    for (var i = 0; i < matrix.length; i++) {
        for (var j = 0; j < matrix[0].length; j++) {
            if (!res[j]) {
                res[j] = [];
            }
            res[j][i] = matrix[i][j];
        }
    }
    console.log(res);
    return res;
};

999. 可以被一步捕获的棋子数

var numRookCaptures = function(board) {
    let x, y; // 车的坐标
    let captureAmount = 0; // 捕获量
    loop: for (let i = 0; i < 8; i++) {
        for (let j = 0; j < 8; j++) {
            if (board[i][j] == "R") {
                x = i;
                y = j;
                break loop;
            }
        }
    }
    // 车在X轴上 位移距离 上右下左顺序
    const dx = [0, 1, 0, -1];
    // 车在Y轴上 位移距离 上右下左顺序
    const dy = [1, 0, -1, 0];

    // 外层循环, 4表示4个方向
    for (let i = 0; i < 4; i++) {
        for (let j = 1;; j++) {
            const moveX = x + j * dx[i];
            const moveY = y + j * dy[i];
            //  判断棋盘边界 且 不可以碰到象
            if ( moveX < 0 || moveX >= 8 || moveY < 0 || moveY >= 8 || board[moveX][moveY] == "B") {
                break;
            }
            if (board[moveX][moveY] == "p") {
                captureAmount++;
                break; // 捕获到卒后,该方向停止前进
            }
        }
    }
    return captureAmount;
};

1030. 距离顺序排列矩阵单元格

var allCellsDistOrder = function(rows, cols, rCenter, cCenter) {
    let x = rCenter;
    let y = cCenter;
    let { abs } = Math;
    let res = [];
    for (let i = 0; i < rows; i++) {
        for (let j = 0; j < cols; j++) {
            res.push([i, j]);
        }
    }
    return res.sort(
        (a, b) => abs(a[0] - x) + abs(a[1] - y) - (abs(b[0] - x) + abs(b[1] - y))
    );
};

1260. 二维网格迁移

var shiftGrid = function(grid, k) {
    while (k--) {
        for (let i = grid.length - 1; i >= 0; i--) {
            grid[(i + 1) % grid.length].unshift(grid[i].pop());
        }
    }
    return grid;
};

1275. 找出井字棋的获胜者

var tictactoe = function(moves) {
    const chess = new Array(3).fill(0).map(() => new Array(3).fill(0));
    moves.forEach((item, index) => {
        let [x, y] = item;
        chess[x][y] = index % 2 ? "B" : "A";
    });
    for (let i = 0; i < 3; i++) {
        let [a, b, c] = chess[i]
        if (a && a == b && b == c) return a
    }

    for (let j = 0; j < 3; j++) {
        let [a, b, c] = [chess[0][j], chess[1][j], chess[2][j]]
        if (a && a == b && b == c) return a
    }

    let [a, b, c] = [chess[0][0], chess[1][1], chess[2][2]]
    if (a && a == b && b == c) return a
    let [j, q, k] = [chess[2][0], chess[1][1], chess[0][2]]
    if (j && j == q && q == k) return j
    if (moves.length < 9) return 'Pending'
    return 'Draw'
};

1337. 矩阵中战斗力最弱的 K 行

var kWeakestRows = function(mat, k) {
    let result = []
    for (let i = 0; i < mat.length; i++) {
        let soldier = mat[i].filter(item => item == 1).length
        result.push([i, soldier])
    }
    return result.sort((a, b) => a[1] - b[1]).splice(0, k).map(item => item[0])
};

1351. 统计有序矩阵中的负数

var countNegatives = function(grid) {
    return grid.flat(Infinity).filter(item => item < 0).length
};
posted @ 2023-01-29 23:36  Echoyya、  阅读(66)  评论(0编辑  收藏  举报