Exercise Beginner: Rotate matrix 90 degrees
Request:
Write a program that rotates the given matrix 90 degrees clockwise. The matrix size is n × m. (1 ≤ n, m ≤ 50, -10^9 <= matrix elements <= 10^9). Print the rotated matrix in the same format like input.
Solution
We will solve this problem with a concrete case, by using the matrix shown bellow:
Example code:
and the result for this specific case should be as follow:
Example code:
In order to reach such result we need the result matrix hight to be same as the input matrix width and the result matrix width to be same with the input matrix height. The new location
In order to have a matrix in code we have to declare a two dimensional array as seen bellow.
Example code:
In order to implement the logic described bellow we will have to write the following code.
Example code:
Write a program that rotates the given matrix 90 degrees clockwise. The matrix size is n × m. (1 ≤ n, m ≤ 50, -10^9 <= matrix elements <= 10^9). Print the rotated matrix in the same format like input.
Solution
We will solve this problem with a concrete case, by using the matrix shown bellow:
Example code:
and the result for this specific case should be as follow:
Example code:
In order to reach such result we need the result matrix hight to be same as the input matrix width and the result matrix width to be same with the input matrix height. The new location
In order to have a matrix in code we have to declare a two dimensional array as seen bellow.
Example code:
In order to implement the logic described bellow we will have to write the following code.
Example code:
Comments
Post a Comment