使用单个字符串"right"或"left"作为第二个参数旋转二维数组矩阵 [英] Rotating a 2D Array Matrix with a single string of 'right' or 'left' as a second argument
问题描述
我正在尝试开发一种将2D阵列旋转90度的解决方案,但是向右或向左旋转将取决于 direction
的第二个参数,这也是我面临的挑战之一其旋转4 x 3矩阵的事实.到目前为止,这是我所拥有的:
I am trying to develop a solution to rotating a 2D array by 90 degrees, but the rotating to right or left would be dependent on the second argument of direction
that is one of my challenges in addition to the fact that its rotating a 4 by 3 matrix. This is what I have thus far:
const solve = (intArray, direction) => {
const matrix = intArray.slice();
for (let rowIndex = 0; rowIndex < matrix.length; rowIndex+= 1) {
for (let columnIndex = rowIndex + 1; columnIndex < matrix.length; columnIndex += 1) {
[
matrix[columnIndex][rowIndex],
matrix[rowIndex][columnIndex],
] = [
matrix[rowIndex][columnIndex],
matrix[columnIndex][rowIndex],
];
}
}
for (let rowIndex = 0; rowIndex < matrix.length; rowIndex += 1) {
for (let columnIndex = 0; columnIndex < matrix.length / 2; columnIndex += 1) {
[
matrix[rowIndex][matrix.length - columnIndex - 1],
matrix[rowIndex][columnIndex],
] = [
matrix[rowIndex][columnIndex],
matrix[rowIndex][matrix.length - columnIndex - 1],
];
}
}
return matrix;
};
solve([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
], "right")
推荐答案
好的,所以这是我解决此问题的方法.我尝试用输入矩阵的行列和列行创建一个矩阵.这意味着从6 x 4矩阵的输入创建4 x 6矩阵.
Ok, so here is my approach to this problem. I tried creating a matrix with row columns and column rows of the input matrix. Meaning that 4 by 6 matrix is created from an input of 6 by 4 matrix.
然后,基于第二个输入,将原始矩阵的值复制到新矩阵的相应位置.
Then, based on the second input, copy the values of the original matrix to the corresponding place to the new matrix.
const solve = (intArray, direction) => {
const matrix = intArray.slice();
//create a matrix of 0s of column * row of the input matrix. 6*4 matrix will be 4*6 matrix
let newMatrix = [];
for (let i = 0; i < matrix[0].length; i += 1){
newMatrix.push([]);
for (let j = 0; j < matrix.length; j += 1){
newMatrix[i].push(0);
}
}
for (let rowIndex = 0; rowIndex < newMatrix.length; rowIndex += 1){
for (let columnIndex = 0; columnIndex < newMatrix[0].length; columnIndex += 1){
if (direction == "right"){
//if right, shift the values clockwise
newMatrix[rowIndex][columnIndex] = matrix[(matrix.length - 1) - columnIndex][rowIndex]
}else if (direction == "left"){
//if left, shift the values anticlockwise
newMatrix[rowIndex][columnIndex] = matrix[columnIndex][(matrix[0].length - 1) - rowIndex]
}
}
}
return newMatrix;
};
solve([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
], "left") // returns [ [ 4, 8, 12 ], [ 3, 7, 11 ], [ 2, 6, 10 ], [ 1, 5, 9 ] ]
solve([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
], "right") // returns [ [ 9, 5, 1 ], [ 10, 6, 2 ], [ 11, 7, 3 ], [ 12, 8, 4 ]
我希望这会有所帮助:)对于类似问题也进行了堆栈交换讨论,因此,如果要优化代码,可以进行检查.> https://math.stackexchange.com/questions/1676441/如何将矩阵的位置旋转90度
I hope this helped :) There was also a stack exchange discussion to a similar problem, so you can check it out if you want to optimize the code. https://math.stackexchange.com/questions/1676441/how-to-rotate-the-positions-of-a-matrix-by-90-degrees
这篇关于使用单个字符串"right"或"left"作为第二个参数旋转二维数组矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!