Home > Web Front-end > JS Tutorial > JavaScript program to rotate a matrix to the right K times

JavaScript program to rotate a matrix to the right K times

王林
Release: 2023-08-24 22:09:06
forward
1503 people have browsed it

将矩阵向右旋转 K 次的 JavaScript 程序

The term "right rotation of a matrix" means moving each column in the matrix to the right. If specified, this operation will be repeated "k" times. In other words, it is a right shift of the matrix that occurs "k" times. This program can be implemented using various programming languages, but a simple yet effective way is to consider using JavaScript to right-rotate a matrix k times.

How to right-rotate a matrix K times?

Right-rotating a matrix k times is simple, which involves moving each column of the matrix k times to the right. To demonstrate this, we perform an example of manually performing a right rotation on a matrix k times.

Example

Let us take a matrix of size N*M and a number K. We have to rotate the matrix to the right k times.

Input matrix: N = 4, M = 4, K = 3
1 2 3 4 
6 7 8 9 
0 9 8 7 
5 4 3 2 
Output matrix:
4 1 2 3
9 6 7 8
7 0 9 8
2 5 4 3
Copy after login

method< /p>

The process of performing k correct rotations may seem easy to understand, but it may be a bit difficult to implement. The method involves copying the elements of each column of row i into a temporary array, up to m-k. Then, we move the elements from k to the end of row i to the beginning. Finally, we copy the elements from the temporary array back to the end of each i row of the matrix.

Let's look at the algorithm of the method we will use.

Algorithm for right-rotating matrix K times

Step 1 - Determine the number of rows and columns in the matrix.

Step 2 - Calculate the number of times each row needs to be moved based on the value of k. This can be done using the modulo operator (%).

Step 3 - For each row in the matrix, create a new array containing the elements that need to be moved.

Step 4 - Use the splice() method to remove the shifted elements from the original row and add them to the beginning of the new row.

Step 5 - Set original row to new row.

Step 6 - Repeat steps 3-5 for each row in the matrix.

Example

In this program, we define a MatrixRotation class, which has two static methods: displayMatrix() and rotateMatrixRight().

displayMatrix() method takes a matrix as input and displays it in the console. It uses a for loop to iterate through each row in the matrix and log it to the console.

rotateMatrixRight() method takes as input a matrix and a number k and returns a new matrix that has been rotated k positions to the right. It uses the same algorithm as the previous answer to perform the rotation.

In the sample usage code, we define an input matrix and k value, and then call the rotateMatrixRight() method to perform the rotation. We display the input and output matrices using the displayMatrix() method.

class MatrixRotation {
   static displayMatrix(matrix) {
      for (let i = 0; i < matrix.length; i++) {
         console.log(matrix[i]);
      }
   }
   static rotateMatrixRight(matrix, k) {
      const numRows = matrix.length;
      const numCols = matrix[0].length;
      // Calculate the number of times each row needs to be shifted
      const shifts = k % numCols;
      // Rotate each row of the matrix
      for (let i = 0; i < numRows; i++) {
         const row = matrix[i];
         // Create a new row that contains the shifted elements
         const newRow = row.slice(numCols - shifts).concat(row.slice(0, numCols - shifts));
         // Set the original row to the new row
         matrix[i] = newRow;
      }
      return matrix;
   }
}
// Example usage
const inputMatrix = [
   [1, 2, 3],
   [4, 5, 6],
   [7, 8, 9]
];
const k = 2;
console.log("Input matrix:");
MatrixRotation.displayMatrix(inputMatrix);
const outputMatrix = MatrixRotation.rotateMatrixRight(inputMatrix, k);
console.log("Output matrix:");
MatrixRotation.displayMatrix(outputMatrix);
Copy after login

in conclusion

Use a few simple steps in JavaScript to right-rotate a matrix k times. The first step is to determine the number of rows and columns in the matrix. The next step is to calculate the number of times each row needs to be shifted right based on the value of k. Once the number of shifts is determined, the program can iterate over each row of the matrix, create a new array containing the shifted elements from the original row, and update the original row with the shifted elements. By following these steps, we can rotate a matrix to the right k times in JavaScript.

The above is the detailed content of JavaScript program to rotate a matrix to the right K times. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template