如何在JavaScript中顺时针旋转给定矩阵一个元素

3

好的,我想实现这个代码在JS中,但我不是很擅长JS,我在打印或测试它时遇到了麻烦...有人能检查一下代码并帮助我console.log它吗?

这是我的目标:

┌     ┐    ┌     ┐ 
|1 2 3|    |4 1 2| 
|4 5 6| -> |7 5 3| 
|7 8 9|    |8 9 6|   
└     ┘    └     ┘

这是我写的代码:

var matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];

function rotateMatrix(matrix) {
 //boundery indication for row and column (index)
 let maxRow = matrix.length - 1;
 let maxCol = matrix.length - 1;
 let row = 0;
 let col = 0;

 //keeps track of ring which we are working with
 while (row < maxRow && col < maxCol) {
  // initial temp starting with 4 being the numb. moving clockwise
  let previous = matrix[row + 1][col];

  //loop through TOP ROW (row 0 ) moving 1 spot
  for (let i = col; i <= maxCol; i++) {
   let current = matrix[row][i]; //current makes space for previous starting w/1
   matrix[row][i] = previous; // putting final num in new spot starting w/4 putting in slot 1
   previous = current; //leaving the loop with previous = 3
  }
  row++; //moves to row index 1

  //starting in row index 1 loop through RIGHT most COLUMN (col 2) moving 1 spot down
  for (let j = row; j <= maxRow; j++) {
   let current = matrix[j][maxCol]; //current makes space for previous starting w/6
   matrix[j][maxCol] = previous; //starting with 3 moving in slot 6
   previous = current; //leaving the loop with previous = 9
  }
  maxCol--; //changes maxCol to index 1

  //loop through the BOTTOM ROW (row 2) moving 1 spot left
  for (let j = maxCol; j >= col; j--) {
   //starting in col index 1 decrementing down to col 0
   let current = matrix[maxRow][j]; //current makes space for previous starting w/8
   matrix[maxRow][j] = previous; //starting with 9 moving in slot 8
   previous = current; //leaving the loop with previous = 7
  }
  maxRow--; //changes maxRow to index 1

  //loop through the LEFT COLUMN (col 0) moving 1 spot up

  for (let i = maxRow; i >= row; i--) {
   //starting in row index 1 decrementing down to row 0
   let current = matrix[i][col]; //current makes space for previous starting w/4
   matrix[i][col] = previous; //starting with 7 moving in slot 4
   previous = current; //leaving the loop with previous = 4
  }
 }
}

///////

console.log(matrix.map(x=>x.join()));
rotateMatrix(matrix);
console.log(matrix.map(x=>x.join()));


嗨,我在这里让你的代码可运行了。在此过程中,我添加了一些前后日志记录。由于它似乎工作正常,我不确定问题是什么。(哦,我必须再次编辑它,因为显然有制表符和空格混合) - tevemadar
1个回答

2

对于顺时针旋转,你必须逆时针绕过去。此外,你存储在开头的元素将在结尾处需要,并且在其他任何地方都不需要。

function p(m){
  for(let l of m)
    console.log(l.join());
  console.log('---');
}
function ccwrectangle(mtx,top,left,bottom,right){
  let elem=mtx[top][left];
  for(let y=top;y<bottom;y++)           // downwards on the left
    mtx[y][left]=mtx[y+1][left];
  for(let x=left;x<right;x++)           // righwards at the bottom
    mtx[bottom][x]=mtx[bottom][x+1];
  for(let y=bottom;y>top;y--)           // upwards on the right
    mtx[y][right]=mtx[y-1][right];
  for(let x=right;x>left+1;x--)         // leftwards on the top
    mtx[top][x]=mtx[top][x-1];
  mtx[top][left+1]=elem;
}

let m1=[[1,2,3],[4,5,6],[7,8,9]];
p(m1);
ccwrectangle(m1,0,0,2,2);
p(m1);

let m2=[[1,2,3,4],[5,6,7,8],[9,'A','B','C'],['D','E','F','0']];
p(m2);
ccwrectangle(m2,0,0,3,3);
ccwrectangle(m2,1,1,2,2);
p(m2);

ccwrectangle() 一次旋转一个“环”,因此对于具有多个“环”的矩阵,它会运行多次。对于方形矩阵的一般方法可能是这样的:

for(let a=0,b=mtx.length-1;b>a;a++,b--)
  ccwrectangle(mtx,a,a,b,b);

即使是针对矩形,也需要分别追踪左右和上下一对。只要有任何一对相遇,循环就会结束。


代码看起来很棒,但对我来说太高级了XD...我重新检查了我的代码并更新了它...但我仍然无法将其注销...有什么帮助吗? - Valiant Lupori Buzzactaxe
@ValiantLuporiBuzzactaxe 我们两种方法的不同之处在于你将所有元素都保留在矩阵中。简化成一行:1234->2134->2314->2341 是你采取的步骤,至少我是这么认为的。同时,我的代码是:1234->2234->2334->2344,然后我将单独存储的 1 放回去,得到了 2341。两种方法都是有效的。 - tevemadar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接