Skip to content

Latest commit

 

History

History
24 lines (23 loc) · 603 Bytes

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

File metadata and controls

24 lines (23 loc) · 603 Bytes
/**
 * @param {number} R
 * @param {number} C
 * @param {number} r0
 * @param {number} c0
 * @return {number[][]}
 */
var allCellsDistOrder = function (R, C, r0, c0) {
    // 存储矩阵坐标的数组
    const mat = [];
    // 构造数组
    for (let row = 0; row < R; ++row) {
        for (let col = 0; col < C; ++col) {
            mat.push([row, col]);
        }
    }
    // 计算坐标到r0,c0的曼哈顿距离
    const instance = (row, col) => Math.abs(row - r0) + Math.abs(col - c0);
    // 排序
    return mat.sort((m1, m2) => instance(...m1) - instance(...m2));
};