问题
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")
回答1:
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 ]
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
回答2:
You could take the switched dimensions for creating new arrays and take adjusted indices for the values.
positive given negative
-------- -------- --------
3 6 1 2 3 4 1
2 5 4 5 6 5 2
1 4 6 3
function rotate(array, positive) {
var l = array.length,
k = array[0].length;
return Array.from(
{ length: k },
(_, j) => Array.from(
{ length: l },
(__, i) => array[positive ? i: l - i - 1][positive ? k - j - 1: j]
)
);
}
rotate([[1, 2, 3], [4, 5, 6]], true).map(a => console.log(...a));
console.log('');
rotate([[1, 2, 3], [4, 5, 6]], false).map(a => console.log(...a));
.as-console-wrapper { max-height: 100% !important; top: 0; }
来源:https://stackoverflow.com/questions/60508865/rotating-a-2d-array-matrix-with-a-single-string-of-right-or-left-as-a-second