计算由相同元素组成的矩阵中的行
给定一个矩阵mat[][] ,任务是计算矩阵中包含相同元素的行数。
例子:
Input: mat[][] = {{1, 1, 1}, {1, 2, 3}, {5, 5, 5}}
Output: 2
All the elements of the first row and all the elements of the third row are the same.
Input: mat[][] = {{1, 2}, {4, 2}}
Output: 0
方法:设置count = 0并开始逐行遍历矩阵,对于特定行,将行中的每个元素添加到集合中并检查size(set) = 1 ,如果是,则更新count = count + 1 。
遍历完所有行后,打印count的值。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count of all identical rows
int countIdenticalRows(vector< vector > mat)
{
int count = 0;
for (int i = 0; i < mat.size(); i++)
{
// HashSet for current row
set hs;
// Traverse the row
for (int j = 0; j < mat[i].size(); j++)
{
// Add all the values of the row in HashSet
hs.insert(mat[i][j]);
}
// Check if size of HashSet = 1
if (hs.size() == 1)
count++;
}
return count;
}
// Driver code
int main()
{
vector< vector > mat = {{ 1, 1, 1 },
{ 1, 2, 3 },
{ 5, 5, 5 }};
cout << countIdenticalRows(mat);
return 0;
}
// This code is contributed by Rituraj Jain
Java
// Java implementation of the approach
import java.util.HashSet;
class GFG {
// Function to return the count of all identical rows
public static int countIdenticalRows(int mat[][])
{
int count = 0;
for (int i = 0; i < mat.length; i++) {
// HashSet for current row
HashSet hs = new HashSet<>();
// Traverse the row
for (int j = 0; j < mat[i].length; j++) {
// Add all the values of the row in HashSet
hs.add(mat[i][j]);
}
// Check if size of HashSet = 1
if (hs.size() == 1)
count++;
}
return count;
}
// Driver code
public static void main(String[] args)
{
int mat[][] = { { 1, 1, 1 },
{ 1, 2, 3 },
{ 5, 5, 5 } };
System.out.print(countIdenticalRows(mat));
}
}
Python3
#Function to return the count of all identical rows
def countIdenticalRows(mat):
count = 0
for i in range(len(mat)):
#HashSet for current row
hs=dict()
#Traverse the row
for j in range(len(mat[i])):
#Add all the values of the row in HashSet
hs[mat[i][j]]=1
#Check if size of HashSet = 1
if (len(hs)== 1):
count+=1
return count
#Driver code
mat= [ [ 1, 1, 1 ],
[ 1, 2, 3 ],
[ 5, 5, 5 ] ]
print(countIdenticalRows(mat))
#This code is contributed by Mohit kumar 29
C#
// C# implementation of
// the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the count
// of all identical rows
public static int countIdenticalRows(int [,]mat)
{
int count = 0;
for (int i = 0;
i < mat.GetLength(0); i++)
{
// HashSet for current row
HashSet hs = new HashSet();
// Traverse the row
for (int j = 0;
j < mat.GetLength(0); j++)
{
// Add all the values
// of the row in HashSet
hs.Add(mat[i, j]);
}
// Check if size of HashSet = 1
if (hs.Count == 1)
count++;
}
return count;
}
// Driver code
public static void Main(String[] args)
{
int [,]mat = {{ 1, 1, 1 },
{ 1, 2, 3 },
{ 5, 5, 5 }};
Console.WriteLine(countIdenticalRows(mat));
}
}
// This code is contributed by Princi Singh
Javascript
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count of all identical rows
int countIdenticalRows(int mat[3][3],int r,int c)
{
int count = 0;
for (int i = 0; i < r; i++)
{
// First element of current row
int first = mat[i][0];
bool allSame = true;
// Compare every element of the current row
// with the first element of the row
for (int j = 1; j < c; j++)
{
// If any element is different
if (mat[i][j] != first)
{
allSame = false;
break;
}
}
// If all the elements of the
// current row were same
if (allSame)
count++;
}
return count;
}
// Driver code
int main()
{
//int mat[3][3] ;
int mat[][3] = { { 1, 1, 2 },
{ 2, 2, 2 },
{ 5, 5, 2 } };
int row_length = sizeof(mat)/sizeof(mat[0]) ;
int col_length = sizeof(mat[0])/sizeof(int) ;
cout << countIdenticalRows(mat, row_length,col_length) << endl;
return 0;
}
// This code is contributed by aishwarya.27
Java
// Java implementation of the approach
class GFG {
// Function to return the count of all identical rows
public static int countIdenticalRows(int mat[][])
{
int count = 0;
for (int i = 0; i < mat.length; i++) {
// First element of current row
int first = mat[i][0];
boolean allSame = true;
// Compare every element of the current row
// with the first element of the row
for (int j = 1; j < mat[i].length; j++) {
// If any element is different
if (mat[i][j] != first) {
allSame = false;
break;
}
}
// If all the elements of the
// current row were same
if (allSame)
count++;
}
return count;
}
// Driver code
public static void main(String[] args)
{
int mat[][] = { { 1, 1, 2 },
{ 2, 2, 2 },
{ 5, 5, 2 } };
System.out.print(countIdenticalRows(mat));
}
}
Python 3
# Python 3 implementation of the approach
# Function to return the count of
# all identical rows
def countIdenticalRows(mat):
count = 0
for i in range(len(mat)):
# First element of current row
first = mat[i][0]
allSame = True
# Compare every element of the current
# row with the first element of the row
for j in range(1, len(mat[i])):
# If any element is different
if (mat[i][j] != first):
allSame = False
break
# If all the elements of the
# current row were same
if (allSame):
count += 1
return count
# Driver code
if __name__ == "__main__":
mat = [[ 1, 1, 2 ],
[2, 2, 2 ],
[5, 5, 2 ]]
print(countIdenticalRows(mat))
# This code is contributed by ita_c
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the count of all identical rows
public static int countIdenticalRows(int [,]mat)
{
int count = 0;
for (int i = 0; i < mat.GetLength(0); i++) {
// First element of current row
int first = mat[i,0];
bool allSame = true;
// Compare every element of the current row
// with the first element of the row
for (int j = 1; j < mat.GetLength(1); j++) {
// If any element is different
if (mat[i,j] != first) {
allSame = false;
break;
}
}
// If all the elements of the
// current row were same
if (allSame)
count++;
}
return count;
}
// Driver code
public static void Main()
{
int [,]mat = { { 1, 1, 2 },
{ 2, 2, 2 },
{ 5, 5, 2 } };
Console.Write(countIdenticalRows(mat));
}
// This code is contributed by Ryuga
}
PHP
Javascript
输出:
2
内存高效方法:设置count = 0并开始逐行遍历矩阵,对于特定行,将该行的第一个元素保存在变量中first并将所有其他元素与first进行比较。如果该行的所有其他元素都等于第一个元素,则更新count = count + 1 。遍历完所有行后,打印count 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count of all identical rows
int countIdenticalRows(int mat[3][3],int r,int c)
{
int count = 0;
for (int i = 0; i < r; i++)
{
// First element of current row
int first = mat[i][0];
bool allSame = true;
// Compare every element of the current row
// with the first element of the row
for (int j = 1; j < c; j++)
{
// If any element is different
if (mat[i][j] != first)
{
allSame = false;
break;
}
}
// If all the elements of the
// current row were same
if (allSame)
count++;
}
return count;
}
// Driver code
int main()
{
//int mat[3][3] ;
int mat[][3] = { { 1, 1, 2 },
{ 2, 2, 2 },
{ 5, 5, 2 } };
int row_length = sizeof(mat)/sizeof(mat[0]) ;
int col_length = sizeof(mat[0])/sizeof(int) ;
cout << countIdenticalRows(mat, row_length,col_length) << endl;
return 0;
}
// This code is contributed by aishwarya.27
Java
// Java implementation of the approach
class GFG {
// Function to return the count of all identical rows
public static int countIdenticalRows(int mat[][])
{
int count = 0;
for (int i = 0; i < mat.length; i++) {
// First element of current row
int first = mat[i][0];
boolean allSame = true;
// Compare every element of the current row
// with the first element of the row
for (int j = 1; j < mat[i].length; j++) {
// If any element is different
if (mat[i][j] != first) {
allSame = false;
break;
}
}
// If all the elements of the
// current row were same
if (allSame)
count++;
}
return count;
}
// Driver code
public static void main(String[] args)
{
int mat[][] = { { 1, 1, 2 },
{ 2, 2, 2 },
{ 5, 5, 2 } };
System.out.print(countIdenticalRows(mat));
}
}
Python3
# Python 3 implementation of the approach
# Function to return the count of
# all identical rows
def countIdenticalRows(mat):
count = 0
for i in range(len(mat)):
# First element of current row
first = mat[i][0]
allSame = True
# Compare every element of the current
# row with the first element of the row
for j in range(1, len(mat[i])):
# If any element is different
if (mat[i][j] != first):
allSame = False
break
# If all the elements of the
# current row were same
if (allSame):
count += 1
return count
# Driver code
if __name__ == "__main__":
mat = [[ 1, 1, 2 ],
[2, 2, 2 ],
[5, 5, 2 ]]
print(countIdenticalRows(mat))
# This code is contributed by ita_c
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the count of all identical rows
public static int countIdenticalRows(int [,]mat)
{
int count = 0;
for (int i = 0; i < mat.GetLength(0); i++) {
// First element of current row
int first = mat[i,0];
bool allSame = true;
// Compare every element of the current row
// with the first element of the row
for (int j = 1; j < mat.GetLength(1); j++) {
// If any element is different
if (mat[i,j] != first) {
allSame = false;
break;
}
}
// If all the elements of the
// current row were same
if (allSame)
count++;
}
return count;
}
// Driver code
public static void Main()
{
int [,]mat = { { 1, 1, 2 },
{ 2, 2, 2 },
{ 5, 5, 2 } };
Console.Write(countIdenticalRows(mat));
}
// This code is contributed by Ryuga
}
PHP
Javascript
输出:
1