【问题标题】:Interview prob - finding coordinates 2d array [closed]面试问题 - 寻找坐标二维数组[关闭]
【发布时间】:2016-04-01 02:48:26
【问题描述】:
  var image = [
  [1, 1, 1, 1, 1, 1, 1],
  [1, 1, 1, 1, 1, 1, 1],
  [1, 1, 1, 0, 0, 0, 1],
  [1, 1, 1, 0, 0, 0, 1],
  [1, 1, 1, 1, 1, 1, 1]
];

有一个每个像素都是白色/黑色的图像。图像是一个简单的二维数组(0 = 黑色,1 = 白色)。众所周知,您获得的图像在白色背景上有一个黑色矩形。你的目标是找到这个矩形并返回它的坐标。

如何遍历二维数组?

【问题讨论】:

  • 只回答面试问题好吗?一路解释​​?还是我应该像做作业一样推迟...
  • 嵌套Array.prototype.findIndex 应该有助于找到左上角。正常循环应该足以找到高度和宽度。
  • @IrkenInvader:如果可以,请回答这个问题,我尝试过使用 indexof js,但并没有真正得到它——所以我知道我需要在什么时候获得帮助`——Stakoverflow 到处都是这样的人——那些准备投反对票并炫耀自己是极客的人,我认为真正的极客只是试图帮助初学者而不是讽刺地离开 cmets。
  • Stack Overflow 也充满了寻求家庭作业帮助的新用户,这是直接从帮助中心引用的:请求家庭作业帮助的问题必须包括您迄今为止所做工作的总结解决问题,并描述您解决问题时遇到的困难。而且,我对你投了反对票,但我敢打赌你还会有更多。

标签: javascript arrays 2d


【解决方案1】:

我不确定您是否只需要知道左上角坐标,还是还需要知道矩形的宽度和高度。我会将两者都包含在我的答案中。

要找到左上角,我会使用以下函数:

function findTopLeftCorner(image) {
    var imageHeight = image[0].length; // Assuming all columns have the same height
    // All columns should have the same height because it is an image
    var imageWidth = image.length;
    for (var x = 0; x < imageWidth; x++) {
        for (var y = 0; y < imageHeight; y++) {
            if (image[x][y] == 0) {
                return {x: x, y: y};
            }
        }
    }
}

要找到宽度和高度,我会使用以下函数:

function getWidthOfRectangle(image, topLeftCorner) {
    var x = topLeftCorner.x;
    while (image[x][topLeftCorner.y] == 0) {
        x++;
    }
    //Now x is the x-coordinate of the first white (1) pixel found
    //Therefore subtract 1 from it to find the last black (0) pixel found
    x--;
    return x - topLeftCorner.x;
}

function getHeightOfRectangle(image, topLeftCorner) {
    var y = topLeftCorner.y;
    while (image[topLeftCorner.x][y] == 0) {
        y++;
    }
    //Now y is the y-coordinate of the first white (1) pixel found
    //Therefore subtract 1 from it to find the last black (0) pixel found
    y--;
    return y - topLeftCorner.y;
}

把它们放在一起:

var topLeftCorner = findTopLeftCorner(image);
var rectangle = {
                 x: topLeftCorner.x,
                 y: topLeftCorner.y,
                 width: getWidthOfRectangle(image, topLeftCorner),
                 height: getHeightOfRectangle(image, topLeftCorner)
};

希望这会有所帮助。这是我的第一个答案,所以如果我犯了错误,请随时在 cmets 中告诉我。

【讨论】:

  • 感谢 Jeremy,感谢您的帮助,它确实让我对如何遍历有了一些了解。
  • 另一种解释 - jsfiddle.net/yg2b5eck 只是另一个角度,我想分享给任何有兴趣的人。可靠的第一个答案杰里米,+1!
  • 坐标更容易找到:获取平面索引var flatIndex = Array.prototype.concat.apply([], image).indexOf(0);。然后得到 x 坐标:var x = flatIndex % image[0].length); 和 y 坐标:var y = Math.floor(flatIndex / image[0].length); 一个工作的jsbin
【解决方案2】:

我提供了一个解决方案。 基本上你需要找出这个二维数组的长度。 Get size of dimensions in array

然后从第一个元素开始遍历它,直到找到第一个零元素,即矩形的左上角。

for(i=0;i<dim[0];i++)
           for(j=0;j<dim[1];j++){
               if(!image[i][j]) {
                     document.getElementById('top-left').innerHTML = i+' '+j;
                        return;
       }
    }

类似地从最后一个元素遍历它,找到第一个零元素,它将是矩形的右下角坐标。

https://jsfiddle.net/wdwgmczr/2/

只要通过这个小提琴。做一些改变看看输出。希望它会有所帮助。

【讨论】:

    【解决方案3】:
    var image = [
      [1, 1, 1, 1, 1, 1, 1],
      [1, 1, 1, 1, 1, 1, 1],
      [1, 1, 1, 0, 0, 0, 1],
      [1, 1, 1, 0, 0, 0, 1],
      [1, 1, 1, 1, 1, 1, 1]
    ];
    
    for (var i = 0; i < image.length; i++) {
        // row is a 1D array
        var row = image[i];
    }
    

    第一个索引将返回第一行,依此类推。

    随后,您可以像访问普通 (1D) 数组一样访问行中的每个像素。

    【讨论】:

    • 似乎您只是在最后回答问题,而不是在整个问题的背景下回答。
    • 我的意图只是展示如何遍历二维数组。我不想给他答案。我相信他在知道如何遍历数组之后能够得到答案。为我糟糕的回答道歉。
    • 你真的这么认为吗?困难的部分是识别嵌入矩形的位置,循环索引是微不足道的。
    • 实际上我做到了,在知道如何遍历二维数组之后 :( 这可能不是最好/优化的方法(我的想法),但它完成了工作。
    猜你喜欢
    • 2016-05-03
    • 2010-12-17
    • 1970-01-01
    • 2021-05-22
    • 2018-07-31
    • 1970-01-01
    • 2012-10-02
    • 2016-02-06
    相关资源
    最近更新 更多