一个关于javascript二维数组的面试题

题目是这样的:
给定一个二维数组,实现一个功能函数 fn,向这个函数中传递这个二维数组的一个坐标,如果这个坐标的值为 ”1“,将返回和这个坐标所有相连的并且坐标值为1坐标。
例如,传递了 fn([3,4])得到的结果为:
[[3,4],[4,4],[5,4],[6,4],[7,4],[8,4],[8,5],[8,6]]

var arr =[
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,1,0,0,0,1,0,0],
    [0,0,0,0,1,0,0,0,1,0,0],
    [0,0,0,0,1,0,0,0,1,0,0],
    [0,0,0,0,1,0,0,0,0,0,0],
    [0,0,0,0,1,0,0,0,0,0,0],
    [0,0,0,0,1,1,1,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0],
] ;
阅读 4k
3 个回答

谢邀,宽度优先遍历即可。供参考,相连条件没给出且当做是横竖方向:

var arr =[ 
[0,0,0,0,0,0,0,0,0,0,0], 
[0,0,0,0,0,0,0,0,0,0,0], 
[0,0,0,0,0,0,0,0,0,0,0], 
[0,0,0,0,1,0,0,0,1,0,0], 
[0,0,0,0,1,0,0,0,1,0,0],
[0,0,0,0,1,0,0,0,1,0,0],
[0,0,0,0,1,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0,0,0],
[0,0,0,0,1,1,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0],
] 

function fn ([x, y]) {
    if (arr[x][y] !== 1) return false
    
    const queue = [[x, y]]
    const memo = arr.map(row => new Array(row.length).fill(false))
    const direction = [
     [-1, 0],
     [1, 0],
     [0, -1],
     [0, 1],
    ]
    
    while(queue.length > 0) {
        const [x, y] = queue.pop()
        direction.forEach(([h, v]) => {
            const newX = x + h
            const newY = y + v
            if (arr[newX][newY] === 1 && !memo[newX][newY]) {
                memo[newX][newY] = true
                queue.push([newX, newY])
            }
        })
    }
    
    const result = []
    for (let i = 0; i < arr.length; i++) {
        for (let j = 0; j < arr[i].length; j++) {
            if(memo[i][j]) {
                result.push([i, j])
            }
        }
    }
    return result
}

console.log(fn([3,4]))

@CRIMX 方法很棒!但是我想修改一点,由于queue只是临时存匹配结果的,还要出队列进行新一轮的匹配所以只要再用一个缓存队列存储过往匹配的成功数据即可,没有必要最后在进行遍历的必要。代码如下:

var arr =[

    [0,0,0,0,0,0,0,0,0,0,0], 
    [0,0,0,0,0,0,0,0,0,0,0], 
    [0,0,0,0,0,0,0,0,0,0,0], 
    [0,0,0,0,1,0,0,0,1,0,0], 
    [0,0,0,0,1,0,0,0,1,0,0],
    [0,0,0,0,1,0,0,0,1,0,0],
    [0,0,0,0,1,0,0,0,0,0,0],
    [0,0,0,0,1,0,0,0,0,0,0],
    [0,0,0,0,1,1,1,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0],
] 

function fn ([x, y]) {
    if (arr[x][y] !== 1) return false
    
    const queue = [[x, y]]
    const result = [[x,y]]
    const memo = arr.map(row => new Array(row.length).fill(false))
    const direction = [
     [-1, 0],
     [1, 0],
     [0, -1],
     [0, 1],
    ]
    
    while(queue.length > 0) {
        const [x, y] = queue.pop()
        direction.forEach(([h, v]) => {
            const newX = x + h
            const newY = y + v
            if (arr[newX][newY] === 1 && !memo[newX][newY]) {
                memo[newX][newY] = true
                queue.push([newX, newY])
                result.push([newX, newY])
            }
        })
    }
    
    return result
} 

@clm1100 如果对英文不排斥的话,推荐一个老外整理的很不错的JavaScript算法方面的GitHub项目

借鉴前两位 对象+递归实现

function fn(point) {
    var memo = {}, result = [], direction = [[-1, 0],[1, 0],[0, -1],[0, 1]]
    function dg([x, y]) {
        result.push(memo[x + "," + y] = [x, y]);
        direction.forEach(([h, v]) => {
            const newX = x + h
            const newY = y + v
            if (arr[newX][newY] === 1 && !memo[newX + "," + newY]) {
                dg([newX, newY]);
            }
        })
    }
    dg(point);
    return result;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题