<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 查找矩陣所有行共有的不同元素 > 原文: [https://www.geeksforgeeks.org/find-distinct-elements-common-rows-matrix/](https://www.geeksforgeeks.org/find-distinct-elements-common-rows-matrix/) 給定一個 n x n 矩陣。 問題是要找到矩陣所有行共有的所有不同元素。 元素可以按任何順序打印。 **示例**: ``` Input : mat[][] = { {2, 1, 4, 3}, {1, 2, 3, 2}, {3, 6, 2, 3}, {5, 2, 5, 3} } Output : 2 3 Input : mat[][] = { {12, 1, 14, 3, 16}, {14, 2, 1, 3, 35}, {14, 1, 14, 3, 11}, {14, 25, 3, 2, 1}, {1, 18, 3, 21, 14} } Output : 1 3 14 ``` **方法 1**:使用三個嵌套循環。 檢查第一行的元素是否存在于所有后續行中。 O(n <sup>3</sup> )的時間復雜度。 可能需要額外的空間來處理重復的元素。 **方法 2**:按升序分別對矩陣的所有行進行排序。 然后對[在 3 個排序的數組](https://www.geeksforgeeks.org/find-common-elements-three-sorted-arrays/)中查找公共元素的問題應用改進的方法。 下面給出了相同的實現。 ## C++ ```cpp // C++ implementation to find distinct elements // common to all rows of a matrix #include <bits/stdc++.h> using namespace std; const int MAX = 100; // function to individually sort // each row in increasing order void sortRows(int mat[][MAX], int n) { ????for (int i=0; i<n; i++) ????????sort(mat[i], mat[i] + n); } // function to find all the common elements void findAndPrintCommonElements(int mat[][MAX], int n) { ????// sort rows individually ????sortRows(mat, n); ????// current column index of each row is stored ????// from where the element is being searched in ????// that row ????int curr_index[n]; ????memset(curr_index, 0, sizeof(curr_index)); ????int f = 0; ????for (; curr_index[0]<n; curr_index[0]++) ????{ ????????// value present at the current column index ????????// of 1st row ????????int value = mat[0][curr_index[0]]; ????????bool present = true; ????????// 'value' is being searched in all the ????????// subsequent rows ????????for (int i=1; i<n; i++) ????????{ ????????????// iterate through all the elements of ????????????// the row from its current column index ????????????// till an element greater than the 'value' ????????????// is found or the end of the row is ????????????// encountered ????????????while (curr_index[i] < n && ???????????????????mat[i][curr_index[i]] <= value) ????????????????curr_index[i]++; ????????????// if the element was not present at the column ????????????// before to the 'curr_index' of the row ????????????if (mat[i][curr_index[i]-1] != value) ????????????????present = false; ????????????// if all elements of the row have ????????????// been traversed ????????????if (curr_index[i] == n) ????????????{ ????????????????f = 1; ????????????????break; ????????????} ????????} ????????// if the 'value' is common to all the rows ????????if (present) ????????????cout << value << " "; ????????// if any row have been completely traversed ????????// then no more common elements can be found ????????if (f == 1) ????????????break; ????} } // Driver program to test above int main() { ????int mat[][MAX] = {? {12, 1, 14, 3, 16}, ????????{14, 2, 1, 3, 35}, ????????{14, 1, 14, 3, 11}, ????????{14, 25, 3, 2, 1}, ????????{1, 18, 3, 21, 14} ????}; ????int n = 5; ????findAndPrintCommonElements(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>

                              哎呀哎呀视频在线观看