作者:王琰杰昔凛 | 来源:互联网 | 2023-07-26 16:01
因此,我正在尝试编写一个函数,该函数可以对角地迭代任何矩阵(无论是正方形还是不正方形),并且可配置从左到右或从右到左的迭代,对角线可以迭代矩阵,并从上到下或从上到下或从上到下迭代。我确实提出了一个函数,该函数成功地沿任一对角线从左到右,从下到上对角线迭代了一个矩阵
function iterateDiagonally(matrix,main,rightToLeft,topToBottom)
{
for(let a = 0,b = matrix.length + matrix[0].length - 1; a {
let x = main ? Math.min(a,matrix.length - 1) : Math.max(0,a - matrix[0].length + 1);
let y = main ? Math.min(matrix[0].length - 1,matrix.length + 1 - a) : Math.min(a,matrix[0].length - 1);
for(let c = 0,d = Math.min(y + 1,main ? x + 1 : matrix.length - x); c {
let eX = main ? x - c : x + c;
let eY = y - c;
console.log(matrix[eX][eY]);
}
}
}
/*
1 4 7
2 5 8
3 6 9
*/
console.log("Along the main diagonal");
iterateDiagonally([[1,2,3],[4,5,6],[7,8,9]],true);
console.log("Along the antidiagonal");
iterateDiagonally([[1,false);
,但是意识到也可以使前两个参数变量变大(已经很庞大的代码)。因此,我正在寻找更干净,紧凑且效率不低的解决方案。谢谢!
我为您创建了一个非常简单的替代实现。
始终从顶部开始,并根据传入的布尔值从顶部或底部开始。然后,我们开始一个迭代循环,始终递增x,并递增或递减y。在每次迭代中,检查该点是否超出范围(这是终止条件)。然后,将该位置的值添加到结果数组。
const board = [
[1,2,3],[4,5,6],[7,8,9]
]
const pointInbounds = (x,y,board) => (
x >= 0
&& x && y >= 0
&& y )
const diagOnalTraversal= (board,bottomToTop=false) => {
let y = bottomToTop ? board.length-1 : 0,x = 0
const values = []
while(pointInbounds(x,board)) {
values.push(board[y][x])
x++
y += bottomToTop ? -1 : 1
}
return values
}
diagonalTraversal(board)
默认情况下,它将从左上角到右下角穿过木板。如果将真布尔值作为第二个参数,它将从左下角开始,然后向右遍历。
,
因此,经过多番思考,我最终完成了自己的工作。如果有人遇到相同的问题,请留在这里。它可能看起来很复杂,但是想法很简单
function iterateDiagonally(matrix,main,rightToLeft,topToBottom)
{
// Move along half of the perimeter,excluding the duplicate corner element
// a - first element of each diagonal
// b - amount of diagonals in total
for(let a = 0,b = matrix.length + matrix[0].length - 1; a {
// For the main diagonal,first move right along the x axis starting with the bottom left element,then move up the y axis
// For the antidiagonal,first move down the y axis starting with the top left element,then move right along the x axis
let x = main ? Math.min(a,matrix.length - 1) : Math.max(0,a - matrix[0].length + 1);
let y = main ? Math.min(matrix[0].length - 1,matrix.length + 1 - a) : Math.min(a,matrix[0].length - 1);
// Invert the starting position and direction of all movement
if(rightToLeft) x = matrix.length - 1 - x,y = matrix[0].length - 1 - y;
let diagOnal= [];
// Move along each diagonal
// c - element of diagonal
// d - amount of elements in diagonal in total
for(let c = 0,d = Math.min(rightToLeft ? matrix[0].length - y : y + 1,main !== rightToLeft ? x + 1 : matrix.length - x); c {
// Invert the diagonal traversal order
let iC = topToBottom !== rightToLeft ? d - 1 - c : c;
// X coordinate of the element
let eX = main !== rightToLeft ? x - iC : x + iC;
// Y coordinate of the element
let eY = rightToLeft ? y + iC : y - iC;
diagonal.push(matrix[eX][eY]);
}
console.log(diagonal.join(","));
}
}
/*
1 4 7
2 5 8
3 6 9
*/
console.log("Parallel main diagonal,left to right,bottom to top");
iterateDiagonally([[1,9]],true,false,false);
console.log("Parallel main diagonal,right to left,top to bottom");
iterateDiagonally([[1,true);
console.log("Parallel main diagonal,true);
console.log("Parallel antidiagonal,false);
console.log("Parallel antidiagonal,true);