GPraveen's blog

By GPraveen, history, 5 weeks ago, In English

1. Rotate MatrixYour title here... ================== 90 Degrees ClockWise Example 1:

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]] Example 2:

Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] Simple Approach Transpose +Reverse: Transpose the Matrix And Reverse Every Row After Transpose

Cpp Solution:

//Rotate Matrix 90 Degrees Clock-Wise

class Solution { public: void rotate(vector<vector>& matrix) { int n=matrix.size();

//Transpose Matrix
    for(int i=0;i<n;i++){
        for(int j=i;j<n;j++){
            swap(matrix[i][j],matrix[j][i]);
        }
    }
    //Reverse each row
    for(int i=0;i<n;i++){
        reverse(matrix[i].begin(),matrix[i].end());
    }
}

};

Java Solution:

import java.util.Arrays;

class Solution { public void rotate(int[][] matrix) { int n = matrix.length;

// Transpose the matrix
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            int temp = matrix[i][j];
            matrix[i][j] = matrix[j][i];
            matrix[j][i] = temp;
        }
    }

    // Reverse each row
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n / 2; j++) {
            int temp = matrix[i][j];
            matrix[i][j] = matrix[i][n - 1 - j];
            matrix[i][n - 1 - j] = temp;
        }
    }
}

}

Python Solution:

================ class Solution: def rotate(self, matrix): n = len(matrix)

# Transpose the matrix
    for i in range(n):
        for j in range(i, n):
            matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

    # Reverse each row
    for i in range(n):
        matrix[i] = matrix[i][::-1]

Java Script Solution:

class Solution { rotate(matrix) { const n = matrix.length;

// Transpose the matrix
    for (let i = 0; i < n; i++) {
        for (let j = i; j < n; j++) {
            [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
        }
    }

    // Reverse each row
    for (let i = 0; i < n; i++) {
        matrix[i].reverse();
    }
}

}

2.Rotate Matrix 90 Degrees Anti-ClockWise:

Example 1:

Input: N = 3 Arr[][] = {{1, 2, 3} {4, 5, 6} {7, 8, 9}} Output: 3 6 9 2 5 8 1 4 7 Explanation: The given matrix is rotated by 90 degree in anti-clockwise direction. Example 2:

Input: N = 4 Arr[][] = {{1, 2, 3, 4} {5, 6, 7, 8} {9, 10, 11, 12} {13, 14, 15, 16}} Output: 4 8 12 16 3 7 11 15 2 6 10 14 1 5 9 13 Explanation: The given matrix is rotated by 90 degree in anti-clockwise direction Simple Approach Transpose and Swap First and Last Row

Cpp Solution:

class Solution{ public:

void rotateMatrix(vector<vector>& arr, int n) { for(int i=0;i<n;i++){ for(int j=i;j<n;j++){ swap(arr[i][j],arr[j][i]); } } int top=0; int bottom=n-1; while(top<bottom){ for(int i=0;i<n;i++){ swap(arr[top][i],arr[bottom][i]); } top++; bottom--; } }

};

Java Solution:

import java.util.Arrays;

class Solution { void rotateMatrix(int[][] arr, int n) { for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { int temp = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = temp; } } int top = 0; int bottom = n — 1; while (top < bottom) { for (int i = 0; i < n; i++) { int temp = arr[top][i]; arr[top][i] = arr[bottom][i]; arr[bottom][i] = temp; } top++; bottom--; } } }

Python Solution:

class Solution: def rotateMatrix(self, arr, n): for i in range(n): for j in range(i, n): arr[i][j], arr[j][i] = arr[j][i], arr[i][j]

top = 0
    bottom = n - 1
    while top < bottom:
        for i in range(n):
            arr[top][i], arr[bottom][i] = arr[bottom][i], arr[top][i]
        top += 1
        bottom -= 1

Java Script Solution:

class Solution { rotateMatrix(arr, n) { for (let i = 0; i < n; i++) { for (let j = i; j < n; j++) { [arr[i][j], arr[j][i]] = [arr[j][i], arr[i][j]]; } } let top = 0; let bottom = n — 1; while (top < bottom) { for (let i = 0; i < n; i++) { [arr[top][i], arr[bottom][i]] = [arr[bottom][i], arr[top][i]]; } top++; bottom--; } } }

  • Vote: I like it
  • -11
  • Vote: I do not like it