沿顺时针方向沿两条对角线翻转给定的矩阵
给定一个大小为M*N的矩阵arr[][] ,其中M是行数, N是列数。任务是通过两条对角线翻转矩阵。翻转矩阵意味着沿对角线顺时针方向旋转矩阵的所有元素。
例子:
Input: arr[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }
Output: { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} }
Explanation: Resultant matrix after flipping the matrix along the main diagonal: { {1, 4, 7}, {2, 5, 8}, {3, 6, 9} }
Resultant matrix after flipping the matrix along the second diagonal: { {9, 8, 7}, {6, 5, 4}, {3, 2, 1} }
Input: arr[][] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} }
Output: { {16, 15, 14, 13}, {12, 11, 10, 9}, {8, 7, 6, 5}, {4, 3, 2, 1} }
方法:这个任务很容易通过观察来解决。可以观察到,生成的矩阵将包含反向顺序的反向行。请按照以下步骤解决问题:
- 遍历矩阵的行,并以相反的顺序交换第一行和最后一行的元素,类似地第二行和倒数第二行,依此类推。
下面是上述代码的实现:
C++
// C++ program for the above approach
#include
using namespace std;
const int m = 4, n = 4;
// Function to flip the matrix along
// both the diagonals
void flip(int matrix[m][n])
{
int i, j, temp;
// Swapping elements
for (i = 0; i < m / 2; i++) {
for (j = 0; j < n; j++) {
temp = matrix[i][j];
matrix[i][j] = matrix[m - 1 - i][n - 1 - j];
matrix[m - 1 - i][n - 1 - j] = temp;
}
}
}
// Function to print the matrix
void show(int matrix[m][n])
{
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
// Driver Code
int main()
{
int matrix[4][4] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
flip(matrix);
show(matrix);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
static int m = 4, n = 4;
// Function to flip the matrix along
// both the diagonals
static void flip(int matrix[][])
{
int i, j, temp;
// Swapping elements
for (i = 0; i < m / 2; i++) {
for (j = 0; j < n; j++) {
temp = matrix[i][j];
matrix[i][j] = matrix[m - 1 - i][n - 1 - j];
matrix[m - 1 - i][n - 1 - j] = temp;
}
}
}
// Function to print the matrix
static void show(int matrix[][])
{
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
System.out.print(matrix[i][j]+ " ");
}
System.out.println();
}
}
// Driver Code
public static void main(String[] args)
{
int matrix[][] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
flip(matrix);
show(matrix);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
m = 4
n = 4
# Function to flip the matrix along
# both the diagonals
def flip(matrix):
i = None
j = None
temp = None
# Swapping elements
for i in range(m // 2):
for j in range(n):
temp = matrix[i][j]
matrix[i][j] = matrix[m - 1 - i][n - 1 - j]
matrix[m - 1 - i][n - 1 - j] = temp
# Function to print the matrix
def show(matrix):
i = None
j = None
for i in range(m):
for j in range(n):
print(matrix[i][j], end = " ")
print("")
# Driver Code
matrix = [ [ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16 ] ]
flip(matrix)
show(matrix)
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG
{
const int m = 4, n = 4;
// Function to flip the matrix along
// both the diagonals
static void flip(int[, ] matrix)
{
int i, j, temp;
// Swapping elements
for (i = 0; i < m / 2; i++) {
for (j = 0; j < n; j++) {
temp = matrix[i, j];
matrix[i, j] = matrix[m - 1 - i, n - 1 - j];
matrix[m - 1 - i, n - 1 - j] = temp;
}
}
}
// Function to print the matrix
static void show(int[, ] matrix)
{
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
// Driver Code
public static void Main()
{
int[, ] matrix = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
flip(matrix);
show(matrix);
}
}
// This code is contributed by ukasp.
Javascript
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1
时间复杂度:O(M*N)
辅助空间:O(1)