【问题标题】:(Javascript - Arrays) Get most left and most right connected character(Javascript - 数组)获取最左边和最右边的连接字符
【发布时间】:2017-11-13 18:44:59
【问题描述】:

我有一个数组(二维矩阵),我想获取

的 x/y 值
  • 最左侧和顶部连接的“1”字符
  • 最右边和最底部连接的'1'-字符

编辑 2.0:

我用参数 x/y 调用我的函数,这是我的起始-'1'-字符的坐标。

10001
00001
11111
01110   --> (x: 1, y: 3)

我的函数会检查 & 上方的列和右侧的列,因此如果有一个字符“1”,它会计算 x 或 y(无论在哪里找到该列)加 1。


我的函数从特定点开始(例如 y: 2, x: 0)


var array = [
 '00000',
 '01111',       --> get position of the most top & right '1'-character (x: 4, y: 1)
 '11000',
 '00000'
]

这是获取“1”字符右上角的函数:

var array = [
 '00000',
 '01111',      
 '11000',
 '00000'
]
  
  
  Array.prototype.get_top_right = function(x_pos, y_pos) {
    var matrix = this, y1= y_pos;
    for (var x1 = x_pos; x1 < this[0].length; x1++) {
      try {
        if (matrix[(y1-1)][x1] == '1') y1--;
        else if (matrix[y1][(x1+1)] != '1') break;
      } catch(e) {}
    }; return [x1,y1]
 }
 
 var result=array.get_top_right(0,2)
 
 console.log(result)

好的。上面的函数似乎工作正常,但现在我想扭转这个过程,以获得我的数组/二维矩阵的最后一个左下角连接的“1”字符。

var array = [
  '00000',
  '01111',      
  '11000', --> get position of the most bottom & left '1'-character (x: 0, y: 2)
  '00000'
]

我不知道如何编辑上面的函数以获取左侧和底部匹配作为结果,而不是像您在上面看到的最右侧和顶部匹配。

编辑 1.0 我之前编写的函数无法正常工作,但看起来像这样:

Array.prototype.get_bottom_left = function(x_pos, y_pos) {
    var matrix = this, y2= y_pos;
    for (var x2 = x_pos; x2 > 0; x2--) {
      try {
        if (matrix[(y2+1)][x2] == '1') y2++;
        if (matrix[y2][(x2-1)] != '1') break;
      } catch(e) {}
    }; return [x2,y2]
 }

使用上面的这个函数和下面的error_array来获取数组的左下角连接字符会导致浏览器崩溃。不错!

  var error_array = [
    '000000',
    '000011',
    '111110',
    '111111'
  ] 

但是,我希望有人可以帮助我更新我的功能...

提前一百万,
问候 - hans。

【问题讨论】:

  • 我可以发誓我昨天看到了同样的问题,但现在找不到了,想象一下。
  • 好的。听起来很疯狂,所以如果你发现了这个问题,如果你能把这个问题的链接提供给我,那就太好了。谢谢!
  • 字符串总是5个字符吗?
  • 如果您指的是数组中几个字符串的长度,是的,长度与所有字符串相同。假设是 5。
  • 我会推荐一个布尔数组,而不是字符串数组。

标签: javascript arrays algorithm


【解决方案1】:

我创建了两个版本的get_bottom_left 方法:

  • bottom_left_up 从 (x, y) 遍历指向左侧和向上
  • bottom_left_down 从 (x, y) 出发,指向右下方。

下面是实现:

Array.prototype.bottom_left_up = function(x, y) {
  if(this[y][x] === '0') {
    return;
  }
  while(y >= 0) {
    while(--x >= 0) {
      if(this[y][x] === '0') {
        return [x + 1, y];
      }
    }
    if(--y === -1 || this[y][this[y].length - 1] === '0') {
      return [0, y + 1];
    }
    x = this[y].length;
  }
};

Array.prototype.bottom_left_down = function(x, y) {
  if(this[y][x] === '0') {
    return;
  }
  while(y < this.length) {
    if(this[y].indexOf('0', x) !== -1) {
      return [x, y];
    }
    if(++y === this.length || this[y][0] === '0') {
      return [x, y - 1];
    }
    x = 0;
  }
};

你看,没有超出范围的保护,可以单独添加没有问题。让我们测试一下逻辑:

var array = [
 '00000',
 '01111',
 '11000',
 '00000'
];
console.log(array.bottom_left_up(2, 1)); // [1, 1]
console.log(array.bottom_left_down(2, 1)); // [0, 2]

var array2 = [
  '000000',
  '000011',
  '111110',
  '111111'
];
console.log(array2.bottom_left_up(3, 3)); // [0, 3]
console.log(array2.bottom_left_down(3, 3)); // [3, 3]

关于方法保护,我不会使用try-catch,我建议如下:

function(x, y) {
  x = parseInt(x, 10);
  y = parseInt(y, 10);
  if(!this.length || isNaN(x) || isNaN(y) || x < 0 || y < 0 || x >= this.length || y >= this[0].length) {
    return;
  }
  // ...
}

因此,您将在 3 种情况下得到“未定义”:空数组、错误参数、未找到。

【讨论】:

  • 您对 try-catch 部分和 1 的看法完全正确。 - 感谢您的代码,但这并不是我想要的结果。我的起始参数 (x/y) 位于一个“1”字符的索引处。 --------->这是我想要得到的结果,就像我在上面问题的函数中提到的那样:var array = [ '00000', '01111', '11000', '00000' ]console.log(array.top_right(0, 2)); // --&gt; [4,1]console.log(array.bottom_left(2, 1)); // [0, 3]
  • @pete234 抱歉,我没有正确理解要求。即使现在我也不确定......顺便说一句,我更新了我的答案并为get_bottom_left 方法提供了两个不同的版本。它们以不同的方向遍历矩阵:左+下和右+上。 get_top_right 方法可以重复相同的逻辑。
【解决方案2】:

这是一个似乎可以处理任何大小的矩阵的函数,但我不太确定您是否希望始终在矩阵中找到第一个“1”,即使它单独在线上右边。。 使用你的数组作为测试参数

    var array = [
    '00000',
    '01111',       
    '11000',
    '00100'];

var error_array = [
    '000000',
    '000011',
    '111111',
    '111111',
    '000000',
    '000010',
    '100000'
    ] 


getLeftBottom(error_array);

function getLeftBottom(testArray)
{
    var firstFound;
    for (var index = testArray.length; index > 0; index--)
    {
        console.log(testArray[index-1]);
        str = testArray[index-1];
        firstFound = str.indexOf("1");
        if (firstFound !== -1)
        {
            console.log("Coordinates: x = {0}, y = {1}", firstFound, index)   // Apparently this doesn't work as I expected in JS but you can still see the coordinates
            break;
        }

    }
}

【讨论】:

    猜你喜欢
    • 2018-01-13
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    相关资源
    最近更新 更多