📜  查找矩阵中给定行的所有排列行

📅  最后修改于: 2021-10-27 08:07:12             🧑  作者: Mango

我们得到一个 m*n 的正整数矩阵和一个行号。任务是找到给定矩阵中的所有行,这些行是给定行元素的排列。还假定每一行中的值是不同的。

例子:

Input : mat[][] = {{3, 1, 4, 2}, 
                   {1, 6, 9, 3},
                   {1, 2, 3, 4},
                   {4, 3, 2, 1}}
        row = 3    
Output: 0, 2
Rows at indexes 0 and 2 are permutations of
row at index 3. 

一个简单的解决方案是对所有行进行一一排序并检查所有行。如果任何行完全等于给定行,则表示当前行是给定行的排列。这种方法的时间复杂度为 O(m*n log n)。

一种有效的方法是使用散列。只需为给定的行创建一个散列集。创建散列集后,遍历剩余的行,并对每一行检查其所有元素是否存在于散列集中。

CPP
// C++ program to find all permutations of a given row
#include
#define MAX 100
 
using namespace std;
 
// Function to find all permuted rows of a given row r
void permutatedRows(int mat[][MAX], int m, int n, int r)
{
    // Creating an empty set
    unordered_set s;
 
    // Count frequencies of elements in given row r
    for (int j=0; j


Java
// Java program to find all permutations of a given row
import java.util.*;
 
class GFG
{
     
static int MAX = 100;
 
// Function to find all permuted rows of a given row r
static void permutatedRows(int mat[][], int m, int n, int r)
{
    // Creating an empty set
    LinkedHashSet s = new LinkedHashSet<>();
 
 
    // Count frequencies of elements in given row r
    for (int j = 0; j < n; j++)
        s.add(mat[r][j]);
 
    // Traverse through all remaining rows
    for (int i = 0; i < m; i++)
    {
        // we do not need to check for given row r
        if (i == r)
            continue;
 
        // initialize hash i.e; count frequencies
        // of elements in row i
        int j;
        for (j = 0; j < n; j++)
            if (!s.contains(mat[i][j]))
                break;
        if (j != n)
        continue;
 
        System.out.print(i+", ");
    }
}
 
// Driver program to run the case
public static void main(String[] args)
{
    int m = 4, n = 4,r = 3;
    int mat[][] = {{3, 1, 4, 2},
                    {1, 6, 9, 3},
                    {1, 2, 3, 4},
                    {4, 3, 2, 1}};
    permutatedRows(mat, m, n, r);
}
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python program to find all
# permutations of a given row
 
# Function to find all
# permuted rows of a given row r
def permutatedRows(mat, m, n, r):
 
 
    # Creating an empty set
    s=set()
 
    # Count frequencies of
    # elements in given row r
    for j in range(n):
        s.add(mat[r][j])   
 
    # Traverse through all remaining rows
    for i in range(m):
 
        # we do not need to check
        # for given row r
        if i == r:
            continue
 
        # initialize hash i.e
        # count frequencies
        # of elements in row i
        for j in range(n):
            if mat[i][j] not in s:
 
                # to avoid the case when last
                # element does not match
                j = j - 2
                break;
        if j + 1 != n:
            continue
        print(i)
             
     
 
# Driver program to run the case
m = 4
n = 4
r = 3
mat = [[3, 1, 4, 2],
       [1, 6, 9, 3],
       [1, 2, 3, 4],
       [4, 3, 2, 1]]
 
permutatedRows(mat, m, n, r)
 
# This code is contributed
# by Upendra Singh Bartwal.


C#
// C# program to find all permutations of a given row
using System;
using System.Collections.Generic;
 
class GFG
{
     
static int MAX = 100;
 
// Function to find all permuted rows of a given row r
static void permutatedRows(int [,]mat, int m, int n, int r)
{
    // Creating an empty set
    HashSet s = new HashSet();
 
 
    // Count frequencies of elements in given row r
    for (int j = 0; j < n; j++)
        s.Add(mat[r, j]);
 
    // Traverse through all remaining rows
    for (int i = 0; i < m; i++)
    {
        // we do not need to check for given row r
        if (i == r)
            continue;
 
        // initialize hash i.e; count frequencies
        // of elements in row i
        int j;
        for (j = 0; j < n; j++)
            if (!s.Contains(mat[i,j]))
                break;
        if (j != n)
        continue;
 
        Console.Write(i+", ");
    }
}
 
// Driver program to run the case
public static void Main(String[] args)
{
    int m = 4, n = 4,r = 3;
    int [,]mat = {{3, 1, 4, 2},
                    {1, 6, 9, 3},
                    {1, 2, 3, 4},
                    {4, 3, 2, 1}};
    permutatedRows(mat, m, n, r);
}
}
 
/* This code contributed by PrinciRaj1992 */


Javascript


CPP
// C++ program to find all permutations of a given row
#include
#define MAX 100
 
using namespace std;
 
// Function to find all permuted rows of a given row r
void permutatedRows(int mat[][MAX], int m, int n, int r)
{
   for (int i=0; i


输出:

0, 2

时间复杂度: O(m*n)
辅助空间: O(n)

解决方案的另一种方法是使用标准模板库 (STL):

CPP

// C++ program to find all permutations of a given row
#include
#define MAX 100
 
using namespace std;
 
// Function to find all permuted rows of a given row r
void permutatedRows(int mat[][MAX], int m, int n, int r)
{
   for (int i=0; i

输出:

0, 2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程