You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于JavaScript实现矩阵行、列及对角线的X值递增转换需求

我来帮你实现剩下的三种转换逻辑,保证代码通用、易读,全部用for循环来完成~

首先先优化下你已有的行转换函数,避免修改原输入矩阵(原代码直接修改了list[ind]会改变原矩阵),同时让逻辑更清晰:

const PLAYER = "X"; // 定义常量,避免硬编码

// 行转换(优化版,不修改原矩阵)
const rowTransform = (matrix) => {
  const result = [];
  const rows = matrix.length;
  if (rows === 0) return result;
  const cols = matrix[0].length;

  for (let i = 0; i < rows; i++) {
    const currentRow = [];
    let inc = 0;
    for (let j = 0; j < cols; j++) {
      if (matrix[i][j] === PLAYER) {
        inc++;
        currentRow.push(inc);
      } else {
        inc = 0;
        currentRow.push(0);
      }
    }
    result.push(currentRow);
  }
  return result;
};

接下来是你需要的另外三种转换实现:

1. 列转换

列转换的逻辑和行转换完全一致,只是遍历顺序变成按列遍历,每一列单独维护计数:

// 列转换
const colTransform = (matrix) => {
  const result = [];
  const rows = matrix.length;
  if (rows === 0) return result;
  const cols = matrix[0].length;

  // 先初始化结果矩阵为全0,后续填充
  for (let i = 0; i < rows; i++) {
    result.push(new Array(cols).fill(0));
  }

  for (let j = 0; j < cols; j++) { // 遍历每一列
    let inc = 0;
    for (let i = 0; i < rows; i++) { // 遍历该列的每一行
      if (matrix[i][j] === PLAYER) {
        inc++;
        result[i][j] = inc;
      } else {
        inc = 0;
        result[i][j] = 0;
      }
    }
  }
  return result;
};

2. 左上到右下(\)对角线转换

这类对角线的核心特征是行号 - 列号 = 固定值,我们按这个固定值分组,对每组内的元素按从上到下的顺序遍历并应用规则:

// 左上到右下(\)对角线转换
const backslashDiagonalTransform = (matrix) => {
  const result = [];
  const rows = matrix.length;
  if (rows === 0) return result;
  const cols = matrix[0].length;

  // 初始化结果矩阵
  for (let i = 0; i < rows; i++) {
    result.push(new Array(cols).fill(0));
  }

  // 行号-列号的范围是 -(cols-1) 到 rows-1
  const minDiff = -(cols - 1);
  const maxDiff = rows - 1;

  for (let diff = minDiff; diff <= maxDiff; diff++) { // 遍历每个对角线分组
    let inc = 0;
    // 找出当前对角线的所有元素,按行递增的顺序遍历
    for (let i = 0; i < rows; i++) {
      const j = i - diff;
      if (j >= 0 && j < cols) { // 确保列号在有效范围内
        if (matrix[i][j] === PLAYER) {
          inc++;
          result[i][j] = inc;
        } else {
          inc = 0;
          result[i][j] = 0;
        }
      }
    }
  }
  return result;
};

3. 右上到左下(/)对角线转换

这类对角线的核心特征是行号 + 列号 = 固定值,同样按这个固定值分组,对每组内的元素按从上到下的顺序遍历:

// 右上到左下(/)对角线转换
const slashDiagonalTransform = (matrix) => {
  const result = [];
  const rows = matrix.length;
  if (rows === 0) return result;
  const cols = matrix[0].length;

  // 初始化结果矩阵
  for (let i = 0; i < rows; i++) {
    result.push(new Array(cols).fill(0));
  }

  // 行号+列号的范围是 0 到 (rows-1)+(cols-1)
  const minSum = 0;
  const maxSum = (rows - 1) + (cols - 1);

  for (let sum = minSum; sum <= maxSum; sum++) { // 遍历每个对角线分组
    let inc = 0;
    // 找出当前对角线的所有元素,按行递增的顺序遍历
    for (let i = 0; i < rows; i++) {
      const j = sum - i;
      if (j >= 0 && j < cols) { // 确保列号在有效范围内
        if (matrix[i][j] === PLAYER) {
          inc++;
          result[i][j] = inc;
        } else {
          inc = 0;
          result[i][j] = 0;
        }
      }
    }
  }
  return result;
};

测试验证

用你提供的输入矩阵测试:

const inputMatrix = [
  ["X", "X", null, null],
  [null, "X", "X", "X"],
  [null, null, "X", "X"],
  ["X", "X", null, null]
];

console.log("行转换结果:", rowTransform(inputMatrix));
console.log("列转换结果:", colTransform(inputMatrix));
console.log("\\对角线转换结果:", backslashDiagonalTransform(inputMatrix));
console.log("/对角线转换结果:", slashDiagonalTransform(inputMatrix));

输出会完全符合你给出的预期结果。

所有函数都用for循环实现,逻辑清晰,支持任意大小的矩形矩阵(不只是4x4),且不会修改原输入矩阵,保证了代码的通用性和安全性。

内容的提问来源于stack exchange,提问作者MansouriAla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:03:08