<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 矩陣對角線的鏡像 > 原文: [https://www.geeksforgeeks.org/mirror-matrix-across-diagonal/](https://www.geeksforgeeks.org/mirror-matrix-across-diagonal/) 給定 2 階 N x N 的二維數組,請打印一個矩陣,該矩陣是對角線上給定樹的鏡像。 我們需要以某種方式打印結果,將對角線上方的三角形的值與其對角線下方的三角形的值交換,就像鏡像交換一樣。 打印以矩陣布局獲得的二維數組。 **示例**: ``` Input : int mat[][] = {{1 2 4 } {5 9 0} { 3 1 7}} Output : 1 5 3 2 9 1 4 0 7 Input : mat[][] = {{1 2 3 4 } {5 6 7 8 } {9 10 11 12} {13 14 15 16} } Output : 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 ``` **此問題的簡單解決方案**占用了額外的空間,我們一對一地遍歷所有對角線(從右到左)。 在對角線遍歷期間,首先將所有元素推入堆棧,然后再次遍歷,然后將對角線的每個元素替換為堆棧元素。 以下是上述想法的實現。 ## C++ ```cpp // Simple CPP program to find mirror of // matrix across diagonal. #include <bits/stdc++.h> using namespace std; const int MAX = 100; void imageSwap(int mat[][MAX], int n) { ????// for diagonal which start from at? ????// first row of matrix ????int row = 0; ????// traverse all top right diagonal ????for (int j = 0; j < n; j++) { ????????// here we use stack for reversing ????????// the element of diagonal ????????stack<int> s; ????????int i = row, k = j; ????????while (i < n && k >= 0)? ????????????s.push(mat[i++][k--]); ????????// push all element back to matrix? ????????// in reverse order ????????i = row, k = j; ????????while (i < n && k >= 0) { ????????????mat[i++][k--] = s.top(); ????????????s.pop(); ????????} ????} ????// do the same process for all the ????// diagonal which start from last ????// column ????int column = n - 1; ????for (int j = 1; j < n; j++) { ????????// here we use stack for reversing? ????????// the elements of diagonal ????????stack<int> s; ????????int i = j, k = column; ????????while (i < n && k >= 0)? ????????????s.push(mat[i++][k--]); ????????// push all element back to matrix? ????????// in reverse order ????????i = j; ????????k = column; ????????while (i < n && k >= 0) { ????????????mat[i++][k--] = s.top(); ????????????s.pop(); ????????} ????} } // Utility function to print a matrix void printMatrix(int mat[][MAX], int n) { ????for (int i = 0; i < n; i++) { ????????for (int j = 0; j < n; j++) ????????????cout << mat[i][j] << " "; ????????cout << endl; ????} } // driver program to test above function int main() { ????int mat[][MAX] = { { 1, 2, 3, 4 }, ?????????????????????{ 5, 6, 7, 8 }, ?????????????????????{ 9, 10, 11, 12 }, ?????????????????????{ 13, 14, 15, 16 } }; ????int n = 4; ????imageSwap(mat, n); ????printMatrix(mat, n); ????return 0; } ``` ## Java ```java // Simple Java program to find mirror of // matrix across diagonal. import java.util.*; class GFG? { ????static int MAX = 100; ????static void imageSwap(int mat[][], int n)? ????{ ????????// for diagonal which start from at? ????????// first row of matrix ????????int row = 0; ????????// traverse all top right diagonal ????????for (int j = 0; j < n; j++)? ????????{ ????????????// here we use stack for reversing ????????????// the element of diagonal ????????????Stack<Integer> s = new Stack<>(); ????????????int i = row, k = j; ????????????while (i < n && k >= 0) ????????????{ ????????????????s.push(mat[i++][k--]); ????????????} ????????????// push all element back to matrix? ????????????// in reverse order ????????????i = row; ????????????k = j; ????????????while (i < n && k >= 0) ????????????{ ????????????????mat[i++][k--] = s.peek(); ????????????????s.pop(); ????????????} ????????} ????????// do the same process for all the ????????// diagonal which start from last ????????// column ????????int column = n - 1; ????????for (int j = 1; j < n; j++) ????????{ ????????????// here we use stack for reversing? ????????????// the elements of diagonal ????????????Stack<Integer> s = new Stack<>(); ????????????int i = j, k = column; ????????????while (i < n && k >= 0) ????????????{ ????????????????s.push(mat[i++][k--]); ????????????} ????????????// push all element back to matrix? ????????????// in reverse order ????????????i = j; ????????????k = column; ????????????while (i < n && k >= 0) ????????????{ ????????????????mat[i++][k--] = s.peek(); ????????????????s.pop(); ????????????} ????????} ????} ????// Utility function to print a matrix ????static void printMatrix(int mat[][], int n)? ????{ ????????for (int i = 0; i < n; i++)? ????????{ ????????????for (int j = 0; j < n; j++) ????????????{ ????????????????System.out.print(mat[i][j] + " "); ????????????} ????????????System.out.println(""); ????????} ????} ????// Driver program to test above function ????public static void main(String[] args) ????{ ????????int mat[][] = {{1, 2, 3, 4}, ????????{5, 6, 7, 8}, ????????{9, 10, 11, 12}, ????????{13, 14, 15, 16}}; ????????int n = 4; ????????imageSwap(mat, n); ????????printMatrix(mat, n); ????} } // This code contributed by Rajput-Ji ``` ## Python3 ```py # Simple Python3 program to find mirror of # matrix across diagonal. MAX = 100 def imageSwap(mat, n): ????# for diagonal which start from at? ????# first row of matrix ????row = 0 ????# traverse all top right diagonal ????for j in range(n): ????????# here we use stack for reversing ????????# the element of diagonal ????????s = [] ????????i = row ????????k = j ????????while (i < n and k >= 0): ????????????s.append(mat[i][k]) ????????????i += 1 ????????????k -= 1 ????????# push all element back to matrix? ????????# in reverse order ????????i = row ????????k = j ????????while (i < n and k >= 0): ????????????mat[i][k] = s[-1] ????????????k -= 1 ????????????i += 1 ????????????s.pop() ????# do the same process for all the ????# diagonal which start from last ????# column ????column = n - 1 ????for j in range(1, n):? ????????# here we use stack for reversing? ????????# the elements of diagonal ????????s = [] ????????i = j ????????k = column ????????while (i < n and k >= 0): ????????????s.append(mat[i][k]) ????????????i += 1 ????????????k -= 1 ????????# push all element back to matrix? ????????# in reverse order ????????i = j ????????k = column ????????while (i < n and k >= 0): ????????????mat[i][k] = s[-1] ????????????i += 1 ????????????k -= 1 ????????????s.pop() # Utility function to pra matrix def printMatrix(mat, n): ????for i in range(n): ????????for j in range(n): ????????????print(mat[i][j], end=" ") ????????print() # Driver code mat = [[1, 2, 3, 4],[5, 6, 7, 8], ????????[9, 10, 11, 12],[13, 14, 15, 16]] n = 4 imageSwap(mat, n) printMatrix(mat, n) # This code is contributed by shubhamsingh10 ``` ## C# ```cs // Simple C# program to find mirror of // matrix across diagonal. using System; using System.Collections.Generic; class GFG? { ????static int MAX = 100; ????static void imageSwap(int [,]mat, int n)? ????{ ????????// for diagonal which start from at? ????????// first row of matrix ????????int row = 0; ????????// traverse all top right diagonal ????????for (int j = 0; j < n; j++)? ????????{ ????????????// here we use stack for reversing ????????????// the element of diagonal ????????????Stack<int> s = new Stack<int>(); ????????????int i = row, k = j; ????????????while (i < n && k >= 0) ????????????{ ????????????????s.Push(mat[i++,k--]); ????????????} ????????????// push all element back to matrix? ????????????// in reverse order ????????????i = row; ????????????k = j; ????????????while (i < n && k >= 0) ????????????{ ????????????????mat[i++,k--] = s.Peek(); ????????????????s.Pop(); ????????????} ????????} ????????// do the same process for all the ????????// diagonal which start from last ????????// column ????????int column = n - 1; ????????for (int j = 1; j < n; j++) ????????{ ????????????// here we use stack for reversing? ????????????// the elements of diagonal ????????????Stack<int> s = new Stack<int>(); ????????????int i = j, k = column; ????????????while (i < n && k >= 0) ????????????{ ????????????????s.Push(mat[i++,k--]); ????????????} ????????????// push all element back to matrix? ????????????// in reverse order ????????????i = j; ????????????k = column; ????????????while (i < n && k >= 0) ????????????{ ????????????????mat[i++,k--] = s.Peek(); ????????????????s.Pop(); ????????????} ????????} ????} ????// Utility function to print a matrix ????static void printMatrix(int [,]mat, int n)? ????{ ????????for (int i = 0; i < n; i++)? ????????{ ????????????for (int j = 0; j < n; j++) ????????????{ ????????????????Console.Write(mat[i,j] + " "); ????????????} ????????????Console.WriteLine(""); ????????} ????} ????// Driver code ????public static void Main(String[] args) ????{ ????????int [,]mat = {{1, 2, 3, 4}, ????????????????????{5, 6, 7, 8}, ????????????????????{9, 10, 11, 12}, ????????????????????{13, 14, 15, 16}}; ????????int n = 4; ????????imageSwap(mat, n); ????????printMatrix(mat, n); ????} } /* This code contributed by PrinciRaj1992 */ ``` **輸出**: ``` 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 ``` **時間復雜度**: O(n * n) **此問題的有效解決方案**是,如果我們觀察到輸出矩陣,則注意到我們只需要交換(mat [i] [j] mat [j] [i])。 以下是上述想法的實現。\ ## C++ ``` // Efficient CPP program to find mirror of // matrix across diagonal. #include <bits/stdc++.h> using namespace std; const int MAX = 100; void imageSwap(int mat[][MAX], int n) { ????// traverse a matrix and swap? ????// mat[i][j] with mat[j][i] ????for (int i = 0; i < n; i++) ????????for (int j = 0; j <= i; j++)? ????????????mat[i][j] = mat[i][j] + mat[j][i] -? ???????????????????????(mat[j][i] = mat[i][j]);??????? } // Utility function to print a matrix void printMatrix(int mat[][MAX], int n) { ????for (int i = 0; i < n; i++) { ????????for (int j = 0; j < n; j++) ????????????cout << mat[i][j] << " "; ????????cout << endl; ????} } // driver program to test above function int main() { ????int mat[][MAX] = { { 1, 2, 3, 4 }, ?????????????????????{ 5, 6, 7, 8 }, ?????????????????????{ 9, 10, 11, 12 }, ?????????????????????{ 13, 14, 15, 16 } }; ????int n = 4; ????imageSwap(mat, n); ????printMatrix(mat, n); ????return 0; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看