Search
 
SCRIPT & CODE EXAMPLE
 

CPP

rotate matrix 90 degrees clockwise

def rotate(matrix):
    rows = len(matrix)
    cols = len(matrix[0])
    res = []
    for i in range(cols):
        temp = []
        for j in range(rows):
            temp.append(matrix[j][i])
        res.append(temp[::-1])

    return res


matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(rotate(matrix))
Comment

rotate matrix by 90 degrees

class Solution 
{
    public void rotateBy90(int [][]mat) 
    {
        // Your code here
      var n = mat[0].length;
      var l = matrixTranspose(mat,n);
      
      for(var i=0;i<n;i++)
      {
        for(var j=n-1;j>=0;j--)
        {
          System.out.print(l[i][j]+" ");
      	}
        System.out.println();
      }
    }   
		public int[][] matrixTranspose(int[][] mat, int n) 
    {
       //Write your code here
      var b = new boolean[n][n];
      for(var i=0;i<n;i++)
      {
        for(var j=0; j<n; j++)
        {
          if(!b[i][i])
          {
            	var t = mat[i][j];
            	mat[i][j] = mat[j][i];
             	mat[j][i] =t;
            	b[i][j] = true;
           		b[j][i]=true;
          }
        }
      }
      return mat;
		}
}
Comment

how to rotate a matrix 90 degrees clockwise

int n=A.size();
    for(int i=0;i<n/2;i++){
        for(int j=i;j<n-i-1;j++){
            int temp=A[i][j];
            A[i][j]=A[n-1-j][i];
            A[n-1-j][i]=A[n-1-i][n-1-j];
            A[n-1-i][n-1-j]=A[j][n-1-i];
            A[j][n-1-i]=temp;
        }
    }
Comment

rotate matrix 90 degrees clockwise in python

#The program defines the square matrix 90 degrees clockwise direction.
m,n  = map(int,input().split())
#m,n are the number of rows and columns.
#m = int(input('rows'))
l = []
for i in range(m):
	x = list(map(int,input().split()))  #for taking the  rows a time and split it append to an empty list		
	l.append()
for i in range(m):
	for j in range(m-1,-1,-1):
		print(l[j][i],end=' ')
	print(end='
')
Comment

rotate matrix 90 degrees clockwise

package Arrays;

import java.util.Arrays;

public class RotateMatrix {
    static int[][] rotate(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;

        int[][] ans = new int[rows][cols];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                ans[i][j] = matrix[j][i];

            }
            reverse(ans[i]);
        }
        return ans;
    }
    
    static void reverse(int[] arr) {
        for (int i = 0; i < arr.length/2; i++) {
            int temp = arr[i];
            arr[i] = arr[arr.length-i-1];
            arr[arr.length-i-1] = temp;
        }
    }

    public static void main(String[] args) {
        int[][] nums = {{1, 2, 3},{4, 5, 6}, {7, 8,9}};
        System.out.println(Arrays.deepToString(rotate(nums)));
    }
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: tower of hanoi 
Cpp :: 1. Two Sum 
Cpp :: heap allocated array in c ++ 
Cpp :: converting int to string c++ 
Cpp :: Array Rotate in c++ 
Cpp :: pointer to pointer c++ 
Cpp :: how to modify 2d array in function c++ 
Cpp :: c++ last element of vector 
Cpp :: queue in cpp 
Cpp :: ? c++ 
Cpp :: cout in c++ 
Cpp :: char input in c++ 
Cpp :: lcm in c++ 
Cpp :: C++ file . 
Cpp :: Restart the computer in c++ after the default time (30) seconds. (Windows) 
Cpp :: how to scan vector in c++ 
Cpp :: C++ Initializing a thread with a class/object 
Cpp :: c shortest path dijkstra 
Cpp :: sfml disable message 
Cpp :: 0-1 knapsack problem implementation of code input array 
Cpp :: convert c++ to mips 
Cpp :: Opengl GLFW basic window 
Cpp :: check whether kth bit is 1 
Cpp :: library management system project in c++ using array 
Cpp :: how to block the screen c++ 
Cpp :: transform c++ 
Cpp :: assign array to array 
Cpp :: c++ start process and get output 
Cpp :: sfml get position 
Cpp :: high school hacking competition 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =