给定一个方阵,我们的任务是在不使用任何额外空间的情况下,将其逆时针旋转180度。
null
例如:
Input : 1 2 3 4 5 6 7 8 9Output : 9 8 7 6 5 4 3 2 1Input : 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output : 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1
方法:1(仅打印旋转矩阵) 这个问题的解决方案是,将矩阵旋转180度,我们可以很容易地遵循这个步骤
Matrix = a00 a01 a02 a10 a11 a12 a20 a21 a22when we rotate it by 90 degreethen matrix isMatrix = a02 a12 a22 a01 a11 a21 a00 a10 a20 when we rotate it by again 90 degree then matrix is Matrix = a22 a21 a20 a12 a11 a10 a02 a01 a00
根据上图 , 我们只需将矩阵旋转180度,就可以打印出 a中给定的矩阵 相反的方式。
C++
// C++ program to rotate a matrix by 180 degrees #include <bits/stdc++.h> #define N 3 using namespace std; // Function to Rotate the matrix by 180 degree void rotateMatrix( int mat[][N]) { // Simply print from last cell to first cell. for ( int i = N - 1; i >= 0; i--) { for ( int j = N - 1; j >= 0; j--) printf ( "%d " , mat[i][j]); printf ( "" ); } } // Driven code int main() { int mat[N][N] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; rotateMatrix(mat); return 0; } |
JAVA
// Java program to rotate a // matrix by 180 degrees import java.util.*; class GFG { static int N = 3 ; // Function to Rotate the // matrix by 180 degree static void rotateMatrix( int mat[][]) { // Simply print from last // cell to first cell. for ( int i = N - 1 ; i >= 0 ; i--) { for ( int j = N - 1 ; j >= 0 ; j--) System.out.print(mat[i][j] + " " ); System.out.println(); } } // Driver Code public static void main(String[] args) { int [][] mat = { { 1 , 2 , 3 }, { 4 , 5 , 6 }, { 7 , 8 , 9 } }; rotateMatrix(mat); } } // This code is contributed by ChitraNayal |
Python3
# Python3 program to # rotate a matrix by # 180 degrees N = 3 ; # Function to Rotate # the matrix by 180 degree def rotateMatrix(mat): # Simply print from # last cell to first cell. i = N - 1 ; while (i > = 0 ): j = N - 1 ; while (j > = 0 ): print (mat[i][j], end = " " ); j = j - 1 ; print (); i = i - 1 ; # Driven code mat = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]]; rotateMatrix(mat); # This code is contributed # by mits |
C#
// C# program to rotate a // matrix by 180 degrees using System; class GFG { static int N = 3; // Function to Rotate the // matrix by 180 degree static void rotateMatrix( int [, ] mat) { // Simply print from last // cell to first cell. for ( int i = N - 1; i >= 0; i--) { for ( int j = N - 1; j >= 0; j--) Console.Write(mat[i, j] + " " ); Console.WriteLine(); } } // Driver Code static public void Main() { int [, ] mat = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; rotateMatrix(mat); } } // This code is contributed by aj_36 |
PHP
<?php // PHP program to rotate // a matrix by 180 degree $N = 3; // Function to Rotate the // matrix by 180 degree function rotateMatrix( $mat ) { global $N ; // Simply print from // last cell to first cell. for ( $i = $N - 1; $i >= 0; $i --) { for ( $j = $N - 1; $j >= 0; $j --) echo $mat [ $i ][ $j ], " " ; echo "" ; } } // Driver Code $mat = array ( array (1, 2, 3), array (4, 5, 6), array (7, 8, 9)); rotateMatrix( $mat ); // This code is contributed by ajit ?> |
Javascript
<script> // Javascript program to rotate a // matrix by 180 degrees N = 3; // Function to Rotate the // matrix by 180 degree function rotateMatrix(mat) { // Simply print from last // cell to first cell. for ( var i = N - 1; i >= 0; i--) { for ( var j = N - 1; j >= 0; j--) document.write(mat[i][j] + " " ); document.write( "<br>" ); } } // Driver Code var mat = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]; rotateMatrix(mat); // This code is contributed by kirti </script> |
输出:
9 8 7 6 5 4 3 2 1
时间复杂性: O(N*N) 辅助空间: O(1)
方法:2(原地旋转) 有四个步骤: 1-找到矩阵的转置。 2-反转转置的列。 3-找到矩阵的转置。 4-转置的反向列
Let the given matrix be1 2 3 45 6 7 89 10 11 1213 14 15 16First we find transpose.1 5 9 132 6 10 143 7 11 154 8 12 16Then we reverse elements of every column.4 8 12 163 7 11 152 6 10 141 5 9 13then transpose again 4 3 2 1 8 7 6 5 12 11 10 916 15 14 13 Then we reverse elements of every column again16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
C++
// C++ program for left rotation of matrix by 180 #include <bits/stdc++.h> using namespace std; #define R 4 #define C 4 // Function to rotate the matrix by 180 degree void reverseColumns( int arr[R][C]) { for ( int i = 0; i < C; i++) for ( int j = 0, k = C - 1; j < k; j++, k--) swap(arr[j][i], arr[k][i]); } // Function for transpose of matrix void transpose( int arr[R][C]) { for ( int i = 0; i < R; i++) for ( int j = i; j < C; j++) swap(arr[i][j], arr[j][i]); } // Function for display the matrix void printMatrix( int arr[R][C]) { for ( int i = 0; i < R; i++) { for ( int j = 0; j < C; j++) cout << arr[i][j] << " " ; cout << '' ; } } // Function to anticlockwise rotate matrix // by 180 degree void rotate180( int arr[R][C]) { transpose(arr); reverseColumns(arr); transpose(arr); reverseColumns(arr); } // Driven code int main() { int arr[R][C] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; rotate180(arr); printMatrix(arr); return 0; } |
JAVA
// Java program for left // rotation of matrix by 180 import java.util.*; class GFG { static int R = 4 , C = 4 , t = 0 ; // Function to rotate the // matrix by 180 degree static void reverseColumns( int arr[][]) { for ( int i = 0 ; i < C; i++) { for ( int j = 0 , k = C - 1 ; j < k; j++, k--) { t = arr[j][i]; arr[j][i] = arr[k][i]; arr[k][i] = t; } } } // Function for transpose of matrix static void transpose( int arr[][]) { for ( int i = 0 ; i < R; i++) { for ( int j = i; j < C; j++) { t = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = t; } } } // Function for display the matrix static void printMatrix( int arr[][]) { for ( int i = 0 ; i < R; i++) { for ( int j = 0 ; j < C; j++) System.out.print(arr[i][j] + " " ); System.out.println(); } } // Function to anticlockwise // rotate matrix by 180 degree static void rotate180( int arr[][]) { transpose(arr); reverseColumns(arr); transpose(arr); reverseColumns(arr); } // Driver Code public static void main(String[] args) { int [][] arr = { { 1 , 2 , 3 , 4 }, { 5 , 6 , 7 , 8 }, { 9 , 10 , 11 , 12 }, { 13 , 14 , 15 , 16 } }; rotate180(arr); printMatrix(arr); } } // This code is contributed by ChitraNayal |
C#
// C# program for left // rotation of matrix by 180 using System; class GFG { static int R = 4, C = 4, t = 0; // Function to rotate the // matrix by 180 degree static void reverseColumns( int [, ] arr) { for ( int i = 0; i < C; i++) { for ( int j = 0, k = C - 1; j < k; j++, k--) { t = arr[j, i]; arr[j, i] = arr[k, i]; arr[k, i] = t; } } } // Function for transpose of matrix static void transpose( int [, ] arr) { for ( int i = 0; i < R; i++) { for ( int j = i; j < C; j++) { t = arr[i, j]; arr[i, j] = arr[j, i]; arr[j, i] = t; } } } // Function for display the matrix static void printMatrix( int [, ] arr) { for ( int i = 0; i < R; i++) { for ( int j = 0; j < C; j++) Console.Write(arr[i, j] + " " ); Console.WriteLine(); } } // Function to anticlockwise // rotate matrix by 180 degree static void rotate180( int [, ] arr) { transpose(arr); reverseColumns(arr); transpose(arr); reverseColumns(arr); } // Driver Code static public void Main() { int [, ] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; rotate180(arr); printMatrix(arr); } } // This code is contributed by ajit |
Python3
# Python3 program for left rotation of matrix by 180 R = 4 C = 4 # Function to rotate the matrix by 180 degree def reverseColumns(arr): for i in range (C): j = 0 k = C - 1 while j < k: t = arr[j][i] arr[j][i] = arr[k][i] arr[k][i] = t j + = 1 k - = 1 # Function for transpose of matrix def transpose(arr): for i in range (R): for j in range (i, C): t = arr[i][j] arr[i][j] = arr[j][i] arr[j][i] = t # Function for display the matrix def printMatrix(arr): for i in range (R): for j in range (C): print (arr[i][j], end = " " ); print (); # Function to anticlockwise rotate matrix # by 180 degree def rotate180(arr): transpose(arr); reverseColumns(arr); transpose(arr); reverseColumns(arr); # Driven code arr = [ [ 1 , 2 , 3 , 4 ], [ 5 , 6 , 7 , 8 ], [ 9 , 10 , 11 , 12 ], [ 13 , 14 , 15 , 16 ] ]; rotate180(arr); printMatrix(arr); |
PHP
<?php // PHP program for left rotation of matrix by 180 $R = 4; $C = 4; // Function to rotate the matrix by 180 degree function reverseColumns(& $arr ) { global $C ; for ( $i = 0; $i < $C ; $i ++) { for ( $j = 0, $k = $C - 1; $j < $k ; $j ++, $k --) { $t = $arr [ $j ][ $i ]; $arr [ $j ][ $i ] = $arr [ $k ][ $i ]; $arr [ $k ][ $i ] = $t ; } } } // Function for transpose of matrix function transpose(& $arr ) { global $R , $C ; for ( $i = 0; $i < $R ; $i ++) { for ( $j = $i ; $j < $C ; $j ++) { $t = $arr [ $i ][ $j ]; $arr [ $i ][ $j ] = $arr [ $j ][ $i ]; $arr [ $j ][ $i ] = $t ; } } } // Function for display the matrix function printMatrix(& $arr ) { global $R , $C ; for ( $i = 0; $i < $R ; $i ++) { for ( $j = 0; $j < $C ; $j ++) { echo $arr [ $i ][ $j ]. " " ; } echo "" ; } } // Function to anticlockwise rotate matrix // by 180 degree function rotate180(& $arr ) { transpose( $arr ); reverseColumns( $arr ); transpose( $arr ); reverseColumns( $arr ); } // Driven code $arr = array ( array ( 1, 2, 3, 4 ), array ( 5, 6, 7, 8 ), array ( 9, 10, 11, 12 ), array ( 13, 14, 15, 16 ) ); rotate180( $arr ); printMatrix( $arr ); return 0; ?> |
Javascript
<script> // Javascript program for left // rotation of matrix by 180 let R = 4, C = 4, t = 0; // Function to rotate the // matrix by 180 degree function reverseColumns(arr) { for (let i = 0; i < C; i++) { for (let j = 0, k = C - 1; j < k; j++, k--) { t = arr[j][i]; arr[j][i] = arr[k][i]; arr[k][i] = t; } } } // Function for transpose of matrix function transpose(arr) { for (let i = 0; i < R; i++) { for (let j = i; j < C; j++) { t = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = t; } } } // Function for display the matrix function printMatrix(arr) { for (let i = 0; i < R; i++) { for (let j = 0; j < C; j++) document.write(arr[i][j] + " " ); document.write( "<br>" ); } } // Function to anticlockwise // rotate matrix by 180 degree function rotate180(arr) { transpose(arr); reverseColumns(arr); transpose(arr); reverseColumns(arr); } // Driver Code let arr = [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [9, 10, 11, 12 ], [13, 14, 15, 16 ] ]; rotate180(arr); printMatrix(arr); //This code is contributed by avanitrachhadiya2155 </script> |
输出:
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
时间复杂性: O(R*C) 辅助空间: O(1) 在上面的代码中,矩阵的转置必须被发现两次,列也必须被反转两次。 所以,我们可以有一个更好的解决方案。
方法:3(位置交换) 在这里,我们交换各个位置的值。
C++
#include <bits/stdc++.h> using namespace std; /** * Reverse Row at specified index in the matrix * @param data matrix * @param index row index */ void reverseRow(vector<vector< int >>& data, int index) { int cols = data[index].size(); for ( int i = 0; i < cols / 2; i++) { int temp = data[index][i]; data[index][i] = data[index][cols - i - 1]; data[index][cols - i - 1] = temp; } } /** * Print Matrix data * @param data matrix */ void printMatrix(vector<vector< int >>& data) { for ( int i = 0; i < data.size(); i++) { for ( int j = 0; j < data[i].size(); j++) { cout << data[i][j] << " " ; } cout << endl; } } /** * Rotate Matrix by 180 degrees * @param data matrix */ void rotateMatrix180(vector<vector< int >>& data) { int rows = data.size(); int cols = data[0].size(); if (rows % 2 != 0) { // If N is odd reverse the middle // row in the matrix reverseRow(data, data.size() / 2); } // Swap the value of matrix [i][j] with // [rows - i - 1][cols - j - 1] for half // the rows size. for ( int i = 0; i <= (rows/2) - 1; i++) { for ( int j = 0; j < cols; j++) { int temp = data[i][j]; data[i][j] = data[rows - i - 1][cols - j - 1]; data[rows - i - 1][cols - j - 1] = temp; } } } // Driver code int main() { vector<vector< int >> data{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 }, { 16, 17, 18, 19, 20 }, { 21, 22, 23, 24, 25 } }; // Rotate Matrix rotateMatrix180(data); // Print Matrix printMatrix(data); return 0; } // This code is contributed by divyeshrabadiya07 |
JAVA
public class GFG { /** * Reverse Row at specified index in the matrix * @param data matrix * @param index row index */ private static void reverseRow( int [][] data, int index) { int cols = data[index].length; for ( int i = 0 ; i < cols / 2 ; i++) { int temp = data[index][i]; data[index][i] = data[index][cols - i - 1 ]; data[index][cols - i - 1 ] = temp; } } /** * Print Matrix data * @param data matrix */ private static void printMatrix( int [][] data) { for ( int i = 0 ; i < data.length; i++) { for ( int j = 0 ; j < data[i].length; j++) { System.out.print(data[i][j] + " " ); } System.out.println( "" ); } } /** * Rotate Matrix by 180 degrees * @param data matrix */ private static void rotateMatrix180( int [][] data) { int rows = data.length; int cols = data[ 0 ].length; if (rows % 2 != 0 ) { //If N is odd reverse the middle row in the matrix reverseRow(data, data.length / 2 ); } //Swap the value of matrix [i][j] with [rows - i - 1][cols - j - 1] for half the rows size. for ( int i = 0 ; i <= (rows/ 2 ) - 1 ; i++) { for ( int j = 0 ; j < cols; j++) { int temp = data[i][j]; data[i][j] = data[rows - i - 1 ][cols - j - 1 ]; data[rows - i - 1 ][cols - j - 1 ] = temp; } } } public static void main(String[] args) { int [][] data = { { 1 , 2 , 3 , 4 , 5 }, { 6 , 7 , 8 , 9 , 10 }, { 11 , 12 , 13 , 14 , 15 }, { 16 , 17 , 18 , 19 , 20 }, { 21 , 22 , 23 , 24 , 25 } }; //Rotate Matrix rotateMatrix180(data); //Print Matrix printMatrix(data); } } |
Python3
# Reverse Row at specified index in the matrix def reverseRow(data, index): cols = len (data[index]) for i in range (cols / / 2 ): temp = data[index][i] data[index][i] = data[index][cols - i - 1 ] data[index][cols - i - 1 ] = temp return data # Print Matrix data def printMatrix(data): for i in range ( len (data)): for j in range ( len (data[ 0 ])): print (data[i][j], end = ' ' ) print () # Rotate Matrix by 180 degrees def rotateMatrix(data): rows = len (data) cols = len (data[ 0 ]) if (rows % 2 ): # If N is odd reverse the middle # row in the matrix data = reverseRow(data, len (data) / / 2 ) # Swap the value of matrix [i][j] with # [rows - i - 1][cols - j - 1] for half # the rows size. for i in range (rows / / 2 ): for j in range (cols): temp = data[i][j] data[i][j] = data[rows - i - 1 ][cols - j - 1 ] data[rows - i - 1 ][cols - j - 1 ] = temp return data # Driver Code data = [ [ 1 , 2 , 3 , 4 , 5 ], [ 6 , 7 , 8 , 9 , 10 ], [ 11 , 12 , 13 , 14 , 15 ], [ 16 , 17 , 18 , 19 , 20 ], [ 21 , 22 , 23 , 24 , 25 ] ] # Rotate Matrix data = rotateMatrix(data) # Print Matrix printMatrix(data) # This code is contributed by rohitsingh07052 |
C#
using System; class GFG { /** * Reverse Row at specified index in the matrix * @param data matrix * @param index row index */ private static void reverseRow( int [, ] data, int index) { int cols = data.GetLength(1); for ( int i = 0; i < cols / 2; i++) { int temp = data[index, i]; data[index, i] = data[index, cols - i - 1]; data[index, cols - i - 1] = temp; } } /** * Print Matrix data * @param data matrix */ private static void printMatrix( int [, ] data) { for ( int i = 0; i < data.GetLength(0); i++) { for ( int j = 0; j < data.GetLength(1); j++) { Console.Write(data[i, j] + " " ); } Console.WriteLine(); } } /** * Rotate Matrix by 180 degrees * @param data matrix */ private static void rotateMatrix180( int [, ] data) { int rows = data.GetLength(0); int cols = data.GetLength(1); if (rows % 2 != 0) { // If N is odd reverse the middle row in the // matrix reverseRow(data, data.GetLength(0) / 2); } // Swap the value of matrix [i][j] with [rows - i - // 1][cols - j - 1] for half the rows size. for ( int i = 0; i <= (rows / 2) - 1; i++) { for ( int j = 0; j < cols; j++) { int temp = data[i, j]; data[i, j] = data[rows - i - 1, cols - j - 1]; data[rows - i - 1, cols - j - 1] = temp; } } } // Driver code public static void Main() { int [, ] data = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 }, { 16, 17, 18, 19, 20 }, { 21, 22, 23, 24, 25 } }; // Rotate Matrix rotateMatrix180(data); // Print Matrix printMatrix(data); } } // This code is contributed by subhammahato348 |
Javascript
<script> // Reverse Row at specified index in the matrix // @param data matrix // @param index row index function reverseRow(data,index) { let cols = data[index].length; for (let i = 0; i < cols / 2; i++) { let temp = data[index][i]; data[index][i] = data[index][cols - i - 1]; data[index][cols - i - 1] = temp; } } /** * Print Matrix data * @param data matrix */ function printMatrix(data) { for (let i = 0; i < data.length; i++) { for (let j = 0; j < data[i].length; j++) { document.write(data[i][j] + " " ); } document.write( "<br>" ); } } /** * Rotate Matrix by 180 degrees * @param data matrix */ function rotateMatrix180(data) { let rows = data.length; let cols = data[0].length; if (rows % 2 != 0) { // If N is odd reverse the middle // row in the matrix reverseRow(data, Math.floor(data.length / 2)); } // Swap the value of matrix [i][j] // with [rows - i - 1][cols - j - 1] // for half the rows size. for (let i = 0; i <= (rows/2) - 1; i++) { for (let j = 0; j < cols; j++) { let temp = data[i][j]; data[i][j] = data[rows - i - 1][cols - j - 1]; data[rows - i - 1][cols - j - 1] = temp; } } } // Driver code let data = [ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8, 9, 10 ], [ 11, 12, 13, 14, 15 ], [ 16, 17, 18, 19, 20 ], [ 21, 22, 23, 24, 25 ] ]; // Rotate Matrix rotateMatrix180(data); // Print Matrix printMatrix(data); // This code is contributed by rag2127 </script> |
输出:
25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
时间复杂性: O(R*C) 辅助空间: O(1)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END