给定一个维度为N * N 的方阵mat[][] ,任务是打印在交换给定矩阵的上三角和下三角的横向反转图像后可以获得的矩阵。
Consider the matrix mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
The lateral image of the lower triangular half of the matrix
The lateral image of the upper triangular half of the matrix
Therefore, following rearrangement of the matrix needs to be performed
例子:
Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Output:
1 8 7
6 5 4
3 2 9
Explanation:
Input: mat[][] = {{1, 2}, {4, 5}}
Output:
1 4
2 5
处理方法:按照以下步骤解决问题:
- 初始化向量数组upDiagonal和lowDiagonal ,以分别存储来自下三角和上三角的矩阵元素的元素。
- 分别使用变量i和j为行和列遍历给定矩阵,并执行以下步骤:
- 如果当前元素在主对角线上,则从本次迭代继续。
- 否则,如果当前元素存在于上三角部分,则将此元素添加到索引abs(i – j)处的upDiagonal 。
- 否则,将当前元素添加到lowDiagonal索引abs(i – j) 处。
- 现在,再次穿过所述基质,并与来自下半和反之亦然的端部的元件替代任何元素存在于上半。
- 完成上述步骤后,打印得到的矩阵。
下面是上述方法的实现:
C++
4
7 8
Java
6
3 2
Python3
1 2 3 1 8 7
4 5 6 to 6 5 4
7 8 9 3 2 9
C#
1 2 3 6 5 4
1 8 7 to 7 8 9
4 5 6 3 2 9
Javascript
// C++ program for the above approach
#include
using namespace std;
// Function to swap laterally inverted
// images of upper and lower triangular
// halves of a given matrix
void ReverseSwap(vector >& mat, int n)
{
// Store the matrix elements from
// upper & lower triangular halves
vector lowerEle[n];
vector upperEle[n];
int index;
// Traverse the matrix mat[][]
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Find the index
index = abs(i - j);
// If current element lies
// on the principal diagonal
if (i == j) {
continue;
}
// If current element lies
// below the principal diagonal
else if (j < i) {
lowerEle[index].push_back(
mat[i][j]);
}
// If current element lies
// above the principal diagonal
else {
upperEle[index].push_back(
mat[i][j]);
}
}
}
// Traverse again to swap values
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Find the index
index = abs(i - j);
// Principal diagonal
if (i == j) {
continue;
}
// Below main diagonal
else if (j < i) {
mat[i][j] = upperEle[index].back();
upperEle[index].pop_back();
}
// Above main diagonal
else {
mat[i][j] = lowerEle[index].back();
lowerEle[index].pop_back();
}
}
}
// Traverse the matrix and print
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
}
// Driver Code
int main()
{
// Given Matrix mat[][]
vector > mat = { { 1, 2 },
{ 4, 5 } };
int N = mat.size();
// Swap the upper and lower
// triangular halves
ReverseSwap(mat, N);
return 0;
}
输出:
// Java program for the above approach
import java.io.*;
class GFG{
// Function to swap laterally inverted
// images of upper and lower triangular
// halves of a given matrix
static void ReverseSwap(int[][] mat, int n)
{
// Store the matrix elements from
// upper & lower triangular halves
int[] lowerEle = new int[n];
int[] upperEle = new int[n];
int index;
// Traverse the matrix mat[][]
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
// Find the index
index = Math.abs(i - j);
// If current element lies
// on the principal diagonal
if (i == j)
{
continue;
}
// If current element lies
// below the principal diagonal
else if (j < i)
{
lowerEle[index] = mat[i][j];
}
// If current element lies
// above the principal diagonal
else
{
upperEle[index] = mat[i][j];
}
}
}
// Traverse again to swap values
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
// Find the index
index = Math.abs(i - j);
// Principal diagonal
if (i == j)
{
continue;
}
// Below main diagonal
else if (j < i)
{
mat[i][j] = upperEle[index];
}
// Above main diagonal
else
{
mat[i][j] = lowerEle[index--];
}
}
}
// Traverse the matrix and print
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
// Driver Code
public static void main(String[] args)
{
// Given Matrix mat[][]
int[][] mat = new int[][]{ { 1, 2 }, { 4, 5 } };
int N = mat.length;
// Swap the upper and lower
// triangular halves
ReverseSwap(mat, N);
}
}
// This code is contributed by Dharanendra L V
时间复杂度: O(N 2 )
辅助空间: O(N 2 )