decode message【BFE.dev】

本文讲述了如何从给定的二维数组中按特定规则(从左上角开始,对角线向下右移动,到达边缘后转对角线上右)解码消息。作者详细描述了代码实现过程,包括边缘检查和循环控制逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The 9th question on BFE.dev. Link here decode message.

I B C A L K A
D R F C A E A
G H O E L A D 

In this question, we are asked to decode message from this two-dimensional array. We should start at top left and then move diagonally down right, when we get to the edge, we should swich to diagonally up right, and move like this way until can’t neither move down right, nor up right.

This question is easy to understand but it’s a little difficult to implement.

So we should firstly check the edge condition, when message.length or message[0].length is zero, return empty string.

Then we init result as an empty string, row and col equals to zero, and directionY equals to 1 which is used to control direction.

After that, we can make a while loop to iterate through the message. And judging conditions in this while loop, should be col less than cols first, which means that we can continue the while loop before we get to the edge of message’s the rightmost col. Then we need to use Logic And with row greater than minus 1 also Logic And row less than rows, in this way we can ensure that row won’t beyond the bondary of message in vertical direction.

After that we use addtion assignment operator here, making result equals to message[row][col] and plus it self. Then let col plus 1 and let row plus directionY, and then we need to control the edge case. When row greater than rows minus 1, it means we have arrived to the bottom of the two dimensional array, so we need to change the direction of it and row minus 2. Else if row less than zero, it meas we have arrived to the top of the dimensional array, so we need to make directionY as 1, and row plus 2.

Then our while loop has finished, and we can return the result.

The whole code is here.

function decode(message) {
  if (message.length === 0 || message[0].length === 0) return ''
  
  const rows = message.length
  const cols = message[0].length

  let result = ''
  let row = 0, col = 0
  let directionY = 1

  while(col < cols && row > -1 && row < rows) {
    result += message[row][col]
    col += 1
    row += directionY

    if (row > rows - 1) {
      directionY = -1
      row -= 2
    } else if (row < 0) {
      directionY = 1
      row += 2
    }
  }
  return result
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值