给定 anxn 矩阵。问题是找到矩阵所有行共有的所有不同元素。元素可以按任何顺序打印。
例子:
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
方法一:使用三个嵌套循环。检查所有后续行中是否存在第一行的元素。 O(n 3 ) 的时间复杂度。可能需要额外的空间来处理重复的元素。方法2:按升序对矩阵的所有行进行单独排序。然后将修改后的方法应用于在 3 个排序数组中查找公共元素的问题。下面给出了相同的实现。
C++
// C++ implementation to find distinct elements
// common to all rows of a matrix
#include
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
Java
// JAVA Code to find distinct elements
// common to all rows of a matrix
import java.util.*;
class GFG {
// function to individually sort
// each row in increasing order
public static void sortRows(int mat[][], int n)
{
for (int i=0; i
Python
# Python3 implementation to find distinct
# elements common to all rows of a matrix
MAX = 100
# function to individually sort
# each row in increasing order
def sortRows(mat, n):
for i in range(0, n):
mat[i].sort();
# function to find all the common elements
def findAndPrintCommonElements(mat, 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
curr_index = [0] * n
for i in range (0, n):
curr_index[i] = 0
f = 0
while(curr_index[0] < n):
# value present at the current
# column index of 1st row
value = mat[0][curr_index[0]]
present = True
# 'value' is being searched in
# all the subsequent rows
for i in range (1, n):
# 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 and
mat[i][curr_index[i]] <= value):
curr_index[i] = curr_index[i] + 1
# 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):
print(value, end = " ")
# if any row have been completely traversed
# then no more common elements can be found
if (f == 1):
break
curr_index[0] = curr_index[0] + 1
# Driver Code
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]]
n = 5
findAndPrintCommonElements(mat, n)
# This code is contributed by iAyushRaj
C#
// C# Code to find distinct elements
// common to all rows of a matrix
using System;
class GFG
{
// function to individually sort
// each row in increasing order
public static void sortRows(int[][] mat, int n)
{
for (int i = 0; i < n; i++)
{
Array.Sort(mat[i]);
}
}
// function to find all the common elements
public static void findAndPrintCommonElements(int[][] mat,
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 = new int[n];
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)
{
Console.Write(value + " ");
}
// if any row have been completely traversed
// then no more common elements can be found
if (f == 1)
{
break;
}
}
}
// Driver Code
public static void Main(string[] args)
{
int[][] mat = new int[][]
{
new int[] {12, 1, 14, 3, 16},
new int[] {14, 2, 1, 3, 35},
new int[] {14, 1, 14, 3, 11},
new int[] {14, 25, 3, 2, 1},
new int[] {1, 18, 3, 21, 14}
};
int n = 5;
findAndPrintCommonElements(mat, n);
}
}
// This code is contributed by Shrikant13
Javascript
C++
// C++ program to find distinct elements
// common to all rows of a matrix
#include
using namespace std;
const int MAX = 100;
// function to individually sort
// each row in increasing order
void findAndPrintCommonElements(int mat[][MAX], int n)
{
unordered_set us;
// map elements of first row
// into 'us'
for (int i=0; i temp;
// mapping elements of current row
// in 'temp'
for (int j=0; j:: iterator itr;
// iterate through all the elements
// of 'us'
for (itr=us.begin(); itr!=us.end(); itr++)
// if an element of 'us' is not present
// into 'temp', then erase that element
// from 'us'
if (temp.find(*itr) == temp.end())
us.erase(*itr);
// if size of 'us' becomes 0,
// then there are no common elements
if (us.size() == 0)
break;
}
// print the common elements
unordered_set:: iterator itr;
for (itr=us.begin(); itr!=us.end(); itr++)
cout << *itr << " ";
}
// Driver program to test above
int main()
{
int mat[][MAX] = { {2, 1, 4, 3},
{1, 2, 3, 2},
{3, 6, 2, 3},
{5, 2, 5, 3} };
int n = 4;
findAndPrintCommonElements(mat, n);
return 0;
}
Python
# Python3 program to find distinct elements
# common to all rows of a matrix
MAX = 100
# function to individually sort
# each row in increasing order
def findAndPrintCommonElements(mat, n):
us = dict()
# map elements of first row
# into 'us'
for i in range(n):
us[mat[0][i]] = 1
for i in range(1, n):
temp = dict()
# mapping elements of current row
# in 'temp'
for j in range(n):
temp[mat[i][j]] = 1
# iterate through all the elements
# of 'us'
for itr in list(us):
# if an element of 'us' is not present
# into 'temp', then erase that element
# from 'us'
if itr not in temp:
del us[itr]
# if size of 'us' becomes 0,
# then there are no common elements
if (len(us) == 0):
break
# prthe common elements
for itr in list(us)[::-1]:
print(itr, end = " ")
# Driver Code
mat = [[2, 1, 4, 3],
[1, 2, 3, 2],
[3, 6, 2, 3],
[5, 2, 5, 3]]
n = 4
findAndPrintCommonElements(mat, n)
# This code is contributed by Mohit Kumar
Javascript
Java
// JAVA Code to find distinct elements
// common to all rows of a matrix
import java.io.*;
import java.util.*;
class GFG {
static void distinct(int matrix[][], int N)
{
// make a empty map
Map ans = new HashMap<>();
// Insert the elements of
// first row in the map and
// initialize with 1
for (int j = 0; j < N; j++) {
ans.put(matrix[0][j], 1);
}
// Traverse the matrix from 2nd row
for (int i = 1; i < N; i++) {
for (int j = 0; j < N; j++) {
// If the element is present in the map
// and is not duplicated in the current row
if (ans.get(matrix[i][j]) != null
&& ans.get(matrix[i][j]) == i) {
// Increment count of the element in
// map by 1
ans.put(matrix[i][j], i + 1);
// If we have reached the last row
if (i == N - 1) {
// Print the element
System.out.print(matrix[i][j]
+ " ");
}
}
}
}
}
/* Driver program to test above function */
public static void main(String[] args)
{
int matrix[][] = { { 2, 1, 4, 3 },
{ 1, 2, 3, 2 },
{ 3, 6, 2, 3 },
{ 5, 2, 5, 3 } };
int n = 4;
distinct(matrix, n);
}
}
// This code is Contributed by Darshit Shukla
C#
// C# code to find distinct elements
// common to all rows of a matrix
using System;
using System.Collections.Generic;
class GFG{
static void distinct(int[,] matrix, int N)
{
// Make a empty map
Dictionary ans = new Dictionary();
// Insert the elements of
// first row in the map and
// initialize with 1
for(int j = 0; j < N; j++)
{
ans[matrix[0, j]] = 1;
}
// Traverse the matrix from 2nd row
for(int i = 1; i < N; i++)
{
for(int j = 0; j < N; j++)
{
// If the element is present in the map
// and is not duplicated in the current row
if (ans.ContainsKey(matrix[i, j]) &&
ans[matrix[i, j]] == i)
{
// Increment count of the element in
// map by 1
ans[matrix[i, j]] = i + 1;
// If we have reached the last row
if (i == N - 1)
{
// Print the element
Console.Write(matrix[i, j] + " ");
}
}
}
}
}
// Driver code
public static void Main(string[] args)
{
int[,] matrix = { { 2, 1, 4, 3 },
{ 1, 2, 3, 2 },
{ 3, 6, 2, 3 },
{ 5, 2, 5, 3 } };
int n = 4;
distinct(matrix, n);
}
}
// This code is contributed by ukasp
Javascript
输出:
1 3 14
时间复杂度: O(n 2 log n),每行大小为 n 需要 O(nlogn) 进行排序,总共有 n 行。
辅助空间: O(n) 用于存储每行的当前列索引。方法3:它使用散列的概念。以下步骤是:
- 映射哈希表中第一行的元素。让它成为hash 。
- Fow 行 = 2 到 n
- 将当前行的每个元素映射到临时哈希表中。让它成为temp 。
- 通过迭代散列和校验中的元素,在散列的元素存在于温度。如果不存在,则从hash 中删除这些元素。
- 当以这种方式处理所有行时,留在散列中的元素就是所需的公共元素。
C++
// C++ program to find distinct elements
// common to all rows of a matrix
#include
using namespace std;
const int MAX = 100;
// function to individually sort
// each row in increasing order
void findAndPrintCommonElements(int mat[][MAX], int n)
{
unordered_set us;
// map elements of first row
// into 'us'
for (int i=0; i temp;
// mapping elements of current row
// in 'temp'
for (int j=0; j:: iterator itr;
// iterate through all the elements
// of 'us'
for (itr=us.begin(); itr!=us.end(); itr++)
// if an element of 'us' is not present
// into 'temp', then erase that element
// from 'us'
if (temp.find(*itr) == temp.end())
us.erase(*itr);
// if size of 'us' becomes 0,
// then there are no common elements
if (us.size() == 0)
break;
}
// print the common elements
unordered_set:: iterator itr;
for (itr=us.begin(); itr!=us.end(); itr++)
cout << *itr << " ";
}
// Driver program to test above
int main()
{
int mat[][MAX] = { {2, 1, 4, 3},
{1, 2, 3, 2},
{3, 6, 2, 3},
{5, 2, 5, 3} };
int n = 4;
findAndPrintCommonElements(mat, n);
return 0;
}
Python
# Python3 program to find distinct elements
# common to all rows of a matrix
MAX = 100
# function to individually sort
# each row in increasing order
def findAndPrintCommonElements(mat, n):
us = dict()
# map elements of first row
# into 'us'
for i in range(n):
us[mat[0][i]] = 1
for i in range(1, n):
temp = dict()
# mapping elements of current row
# in 'temp'
for j in range(n):
temp[mat[i][j]] = 1
# iterate through all the elements
# of 'us'
for itr in list(us):
# if an element of 'us' is not present
# into 'temp', then erase that element
# from 'us'
if itr not in temp:
del us[itr]
# if size of 'us' becomes 0,
# then there are no common elements
if (len(us) == 0):
break
# prthe common elements
for itr in list(us)[::-1]:
print(itr, end = " ")
# Driver Code
mat = [[2, 1, 4, 3],
[1, 2, 3, 2],
[3, 6, 2, 3],
[5, 2, 5, 3]]
n = 4
findAndPrintCommonElements(mat, n)
# This code is contributed by Mohit Kumar
Javascript
输出:
3 2
时间复杂度: O(n 2 )
空间复杂度: O(n)
方法四:使用地图
- 插入地图中第一行的所有元素。
- 现在我们检查地图中存在的元素是否存在于每一行中。
- 如果元素存在于地图中并且在当前行中没有重复,那么我们将地图中元素的计数增加 1。
- 如果我们在遍历时到达最后一行并且如果元素在此之前出现 (N-1) 次,则我们打印该元素。
Java
// JAVA Code to find distinct elements
// common to all rows of a matrix
import java.io.*;
import java.util.*;
class GFG {
static void distinct(int matrix[][], int N)
{
// make a empty map
Map ans = new HashMap<>();
// Insert the elements of
// first row in the map and
// initialize with 1
for (int j = 0; j < N; j++) {
ans.put(matrix[0][j], 1);
}
// Traverse the matrix from 2nd row
for (int i = 1; i < N; i++) {
for (int j = 0; j < N; j++) {
// If the element is present in the map
// and is not duplicated in the current row
if (ans.get(matrix[i][j]) != null
&& ans.get(matrix[i][j]) == i) {
// Increment count of the element in
// map by 1
ans.put(matrix[i][j], i + 1);
// If we have reached the last row
if (i == N - 1) {
// Print the element
System.out.print(matrix[i][j]
+ " ");
}
}
}
}
}
/* Driver program to test above function */
public static void main(String[] args)
{
int matrix[][] = { { 2, 1, 4, 3 },
{ 1, 2, 3, 2 },
{ 3, 6, 2, 3 },
{ 5, 2, 5, 3 } };
int n = 4;
distinct(matrix, n);
}
}
// This code is Contributed by Darshit Shukla
C#
// C# code to find distinct elements
// common to all rows of a matrix
using System;
using System.Collections.Generic;
class GFG{
static void distinct(int[,] matrix, int N)
{
// Make a empty map
Dictionary ans = new Dictionary();
// Insert the elements of
// first row in the map and
// initialize with 1
for(int j = 0; j < N; j++)
{
ans[matrix[0, j]] = 1;
}
// Traverse the matrix from 2nd row
for(int i = 1; i < N; i++)
{
for(int j = 0; j < N; j++)
{
// If the element is present in the map
// and is not duplicated in the current row
if (ans.ContainsKey(matrix[i, j]) &&
ans[matrix[i, j]] == i)
{
// Increment count of the element in
// map by 1
ans[matrix[i, j]] = i + 1;
// If we have reached the last row
if (i == N - 1)
{
// Print the element
Console.Write(matrix[i, j] + " ");
}
}
}
}
}
// Driver code
public static void Main(string[] args)
{
int[,] matrix = { { 2, 1, 4, 3 },
{ 1, 2, 3, 2 },
{ 3, 6, 2, 3 },
{ 5, 2, 5, 3 } };
int n = 4;
distinct(matrix, n);
}
}
// This code is contributed by ukasp
Javascript
输出:
2 3
时间复杂度: O(n 2 )
空间复杂度: O(n)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。