给定一个二进制矩阵,打印给定矩阵的所有唯一行。
例子:
Input: {0, 1, 0, 0, 1} {1, 0, 1, 1, 0} {0, 1, 0, 0, 1} {1, 1, 1, 0, 0}Output: 0 1 0 0 1 1 0 1 1 0 1 1 1 0 0 Explanation: The rows are r1={0, 1, 0, 0, 1}, r2={1, 0, 1, 1, 0}, r3={0, 1, 0, 0, 1}, r4={1, 1, 1, 0, 0}, As r1 = r3, remove r3and print the other rows.Input: {0, 1, 0} {1, 0, 1} {0, 1, 0}Output: 0 1 0 1 0 1Explanation: The rows are r1={0, 1, 0}, r2={1, 0, 1}, r3={0, 1, 0} As r1 = r3,remove r3 and print the other rows.
方法1 : 该方法解释了解决上述问题的简单方法。
方法: 一种简单的方法是用所有处理过的行检查每一行。打印第一行。现在,从第二行开始,对于每一行,将该行与已处理的行进行比较。如果该行与任何已处理的行匹配,请跳过它,否则请打印它。
算法:
- 按行遍历矩阵
- 对于每一行,检查是否有任何类似的行小于当前索引。
- 如果任何两行相似,则不要打印该行。
- 否则打印行。
实施:
C++
// Given a binary matrix of M X N of integers, // you need to return only unique rows of binary array #include <bits/stdc++.h> using namespace std; #define ROW 4 #define COL 5 // The main function that prints // all unique rows in a given matrix. void findUniqueRows( int M[ROW][COL]) { //Traverse through the matrix for ( int i=0; i<ROW; i++) { int flag=0; //check if there is similar column //is already printed, i.e if i and //jth column match. for ( int j=0; j<i; j++) { flag=1; for ( int k=0; k<=COL; k++) if (M[i][k]!=M[j][k]) flag=0; if (flag==1) break ; } //if no row is similar if (flag==0) { //print the row for ( int j=0; j<COL; j++) cout<<M[i][j]<< " " ; cout<<endl; } } } // Driver Code int main() { int M[ROW][COL] = {{0, 1, 0, 0, 1}, {1, 0, 1, 1, 0}, {0, 1, 0, 0, 1}, {1, 0, 1, 0, 0}}; findUniqueRows(M); return 0; } |
JAVA
// Given a binary matrix of M X N // of integers, you need to return // only unique rows of binary array class GFG{ static int ROW = 4 ; static int COL = 5 ; // Function that prints all // unique rows in a given matrix. static void findUniqueRows( int M[][]) { // Traverse through the matrix for ( int i = 0 ; i < ROW; i++) { int flag = 0 ; // Check if there is similar column // is already printed, i.e if i and // jth column match. for ( int j = 0 ; j < i; j++) { flag = 1 ; for ( int k = 0 ; k < COL; k++) if (M[i][k] != M[j][k]) flag = 0 ; if (flag == 1 ) break ; } // If no row is similar if (flag == 0 ) { // Print the row for ( int j = 0 ; j < COL; j++) System.out.print(M[i][j] + " " ); System.out.println(); } } } // Driver Code public static void main(String[] args) { int M[][] = { { 0 , 1 , 0 , 0 , 1 }, { 1 , 0 , 1 , 1 , 0 }, { 0 , 1 , 0 , 0 , 1 }, { 1 , 0 , 1 , 0 , 0 } }; findUniqueRows(M); } } // This code is contributed by mark_85 |
Python3
# Given a binary matrix of M X N of # integers, you need to return only # unique rows of binary array ROW = 4 COL = 5 # The main function that prints # all unique rows in a given matrix. def findUniqueRows(M): # Traverse through the matrix for i in range (ROW): flag = 0 # Check if there is similar column # is already printed, i.e if i and # jth column match. for j in range (i): flag = 1 for k in range (COL): if (M[i][k] ! = M[j][k]): flag = 0 if (flag = = 1 ): break # If no row is similar if (flag = = 0 ): # Print the row for j in range (COL): print (M[i][j], end = " " ) print () # Driver Code if __name__ = = '__main__' : M = [ [ 0 , 1 , 0 , 0 , 1 ], [ 1 , 0 , 1 , 1 , 0 ], [ 0 , 1 , 0 , 0 , 1 ], [ 1 , 0 , 1 , 0 , 0 ] ] findUniqueRows(M) # This code is contributed by mohit kumar 29 |
C#
// Given a binary matrix of M X N // of integers, you need to return // only unique rows of binary array using System; class GFG{ static int ROW = 4; static int COL = 5; // Function that prints all // unique rows in a given matrix. static void findUniqueRows( int [,] M) { // Traverse through the matrix for ( int i = 0; i < ROW; i++) { int flag = 0; // Check if there is similar column // is already printed, i.e if i and // jth column match. for ( int j = 0; j < i; j++) { flag = 1; for ( int k = 0; k < COL; k++) if (M[i, k] != M[j, k]) flag = 0; if (flag == 1) break ; } // If no row is similar if (flag == 0) { // Print the row for ( int j = 0; j < COL; j++) Console.Write(M[i, j] + " " ); Console.WriteLine(); } } } // Driver code static void Main() { int [,] M = { { 0, 1, 0, 0, 1 }, { 1, 0, 1, 1, 0 }, { 0, 1, 0, 0, 1 }, { 1, 0, 1, 0, 0 } }; findUniqueRows(M); } } // This code is contributed by divyeshrabadiya07 |
Javascript
<script> // Given a binary matrix of M X N // of integers, you need to return // only unique rows of binary array let ROW = 4; let COL = 5; // Function that prints all // unique rows in a given matrix. function findUniqueRows(M) { // Traverse through the matrix for (let i = 0; i < ROW; i++) { let flag = 0; // Check if there is similar column // is already printed, i.e if i and // jth column match. for (let j = 0; j < i; j++) { flag = 1; for (let k = 0; k < COL; k++) if (M[i][k] != M[j][k]) flag = 0; if (flag == 1) break ; } // If no row is similar if (flag == 0) { // Print the row for (let j = 0; j < COL; j++) document.write(M[i][j] + " " ); document.write( "<br>" ); } } } // Driver Code let M = [ [ 0, 1, 0, 0, 1 ], [ 1, 0, 1, 1, 0 ], [ 0, 1, 0, 0, 1 ], [ 1, 0, 1, 0, 0 ] ] findUniqueRows(M) // This code is contributed by unknown2108 </script> |
输出:
0 1 0 0 1 1 0 1 1 0 1 0 1 0 0
复杂性分析:
- 时间复杂性: O(第2行x列)。 因此,对于每一行,检查是否有任何其他类似的行。所以时间复杂度是O(行^2 x列)。
- 辅助空间: O(1)。 因为不需要额外的空间。
方法2 : 这种方法使用 二叉搜索树 解决上述操作。二叉搜索树是一种基于节点的二叉树数据结构,具有以下特性:
- 节点的左子树只包含键小于节点键的节点。
- 节点的右子树仅包含键大于节点键的节点。
- 左子树和右子树也必须是二叉搜索树。
- 不能有重复的节点。
二叉搜索树的上述属性提供了键之间的排序,以便快速完成搜索、最小值和最大值等操作。如果没有顺序,那么我们可能需要比较每个键来搜索给定的键。
方法: 这个过程必须从找到每一行的十进制等价物开始,并将它们插入BST。正如我们所知,BST的每个节点将包含两个字段,一个字段表示十进制值,另一个字段表示行号。如果节点重复,则不能插入该节点。最后,遍历BST并打印相应的行。
算法:
- 创建一个不能存储重复元素的BST。创建一个函数,将行转换为十进制,并将十进制值转换为二进制数组。
- 遍历矩阵并将行插入BST。
- 遍历BST(按顺序遍历),将十进制数转换为二进制数组并打印。
实施:
C++14
// Given a binary matrix of M X N of integers, // you need to return only unique rows of binary array #include <bits/stdc++.h> using namespace std; #define ROW 4 #define COL 5 class BST { int data; BST *left, *right; public : // Default constructor. BST(); // Parameterized constructor. BST( int ); // Insert function. BST* Insert(BST *, int ); // Inorder traversal. void Inorder(BST *); }; //convert array to decimal int convert( int arr[]) { int sum=0; for ( int i=0; i<COL; i++) { sum+= pow (2,i)*arr[i]; } return sum; } //print the column represented as integers void print( int p) { for ( int i=0; i<COL; i++) { cout<<p%2<< " " ; p/=2; } cout<<endl; } // Default Constructor definition. BST :: BST() : data(0), left(NULL), right(NULL){} // Parameterized Constructor definition. BST :: BST( int value) { data = value; left = right = NULL; } // Insert function definition. BST* BST :: Insert(BST *root, int value) { if (!root) { // Insert the first node, if root is NULL. return new BST(value); } //if the value is present if (value == root->data) return root; // Insert data. if (value > root->data) { // Insert right node data, if the 'value' // to be inserted is greater than 'root' node data. // Process right nodes. root->right = Insert(root->right, value); } else { // Insert left node data, if the 'value' // to be inserted is greater than 'root' node data. // Process left nodes. root->left = Insert(root->left, value); } // Return 'root' node, after insertion. return root; } // Inorder traversal function. // This gives data in sorted order. void BST :: Inorder(BST *root) { if (!root) { return ; } Inorder(root->left); print( root->data ); Inorder(root->right); } // The main function that prints // all unique rows in a given matrix. void findUniqueRows( int M[ROW][COL]) { BST b, *root = NULL; //Traverse through the matrix for ( int i=0; i<ROW; i++) { //insert the row into BST root=b.Insert(root,convert(M[i])); } //print b.Inorder(root); } // Driver Code int main() { int M[ROW][COL] = {{0, 1, 0, 0, 1}, {1, 0, 1, 1, 0}, {0, 1, 0, 0, 1}, {1, 0, 1, 0, 0}}; findUniqueRows(M); return 0; } |
JAVA
// Given a binary matrix of M X N of integers, // you need to return only unique rows of binary array import java.util.*; class GFG{ static class BST { int data; BST left,right; BST( int v){ this .data = v; this .left = this .right = null ; } } final static int ROW = 4 ; final static int COL = 5 ; // convert array to decimal static int convert( int arr[]) { int sum = 0 ; for ( int i = 0 ; i < COL; i++) { sum += Math.pow( 2 ,i)*arr[i]; } return sum; } // print the column represented as integers static void print( int p) { for ( int i = 0 ; i < COL; i++) { System.out.print(p% 2 + " " ); p /= 2 ; } System.out.println(); } // Insert function definition. static BST Insert(BST root, int value) { if (root == null ) { // Insert the first node, if root is null. return new BST(value); } //if the value is present if (value == root.data) return root; // Insert data. if (value > root.data) { // Insert right node data, if the 'value' // to be inserted is greater than 'root' node data. // Process right nodes. root.right = Insert(root.right, value); } else { // Insert left node data, if the 'value' // to be inserted is greater than 'root' node data. // Process left nodes. root.left = Insert(root.left, value); } // Return 'root' node, after insertion. return root; } // Inorder traversal function. // This gives data in sorted order. static void Inorder(BST root) { if (root == null ) { return ; } Inorder(root.left); print( root.data ); Inorder(root.right); } // The main function that prints // all unique rows in a given matrix. static void findUniqueRows( int M[][]) { BST b, root = null ; // Traverse through the matrix for ( int i = 0 ; i < ROW; i++) { // insert the row into BST root=Insert(root, convert(M[i])); } //print Inorder(root); } // Driver Code public static void main(String[] args) { int M[][] = {{ 0 , 1 , 0 , 0 , 1 }, { 1 , 0 , 1 , 1 , 0 }, { 0 , 1 , 0 , 0 , 1 }, { 1 , 0 , 1 , 0 , 0 }}; findUniqueRows(M); } } // This code is contributed by Rajput-Ji |
C#
// Given a binary matrix of M X N of integers, // you need to return only unique rows of binary array using System; using System.Collections.Generic; public class GFG{ public class BST { public int data; public BST left,right; public BST( int v){ this .data = v; this .left = this .right = null ; } } readonly static int ROW = 4; readonly static int COL = 5; // convert array to decimal static int convert( int []arr) { int sum = 0; for ( int i = 0; i < COL; i++) { sum += ( int )Math.Pow(2,i)*arr[i]; } return sum; } // print the column represented as integers static void print( int p) { for ( int i = 0; i < COL; i++) { Console.Write(p%2+ " " ); p /= 2; } Console.WriteLine(); } // Insert function definition. static BST Insert(BST root, int value) { if (root == null ) { // Insert the first node, if root is null. return new BST(value); } // if the value is present if (value == root.data) return root; // Insert data. if (value > root.data) { // Insert right node data, if the 'value' // to be inserted is greater than 'root' node data. // Process right nodes. root.right = Insert(root.right, value); } else { // Insert left node data, if the 'value' // to be inserted is greater than 'root' node data. // Process left nodes. root.left = Insert(root.left, value); } // Return 'root' node, after insertion. return root; } // Inorder traversal function. // This gives data in sorted order. static void Inorder(BST root) { if (root == null ) { return ; } Inorder(root.left); print( root.data ); Inorder(root.right); } public static int [] GetRow( int [,] matrix, int row) { var rowLength = matrix.GetLength(1); var rowVector = new int [rowLength]; for ( var i = 0; i < rowLength; i++) rowVector[i] = matrix[row, i]; return rowVector; } // The main function that prints // all unique rows in a given matrix. static void findUniqueRows( int [,]M) { BST b, root = null ; // Traverse through the matrix for ( int i = 0; i < ROW; i++) { // insert the row into BST int [] row = GetRow(M,i); root=Insert(root, convert(row)); } //print Inorder(root); } // Driver Code public static void Main(String[] args) { int [,]M = {{0, 1, 0, 0, 1}, {1, 0, 1, 1, 0}, {0, 1, 0, 0, 1}, {1, 0, 1, 0, 0}}; findUniqueRows(M); } } // This code contributed by Rajput-Ji |
Javascript
<script> // Given a binary matrix of M X N of integers, // you need to return only unique rows of binary array var ROW = 4 var COL = 5 class BST { constructor(data) { this .data = data; this .left = null ; this .right = null ; } // Insert function definition. Insert(root, value) { if (!root) { // Insert the first node, if root is NULL. return new BST(value); } //if the value is present if (value == root.data) return root; // Insert data. if (value > root.data) { // Insert right node data, if the 'value' // to be inserted is greater than 'root' node data. // Process right nodes. root.right = this .Insert(root.right, value); } else { // Insert left node data, if the 'value' // to be inserted is greater than 'root' node data. // Process left nodes. root.left = this .Insert(root.left, value); } // Return 'root' node, after insertion. return root; } // Inorder traversal function. // This gives data in sorted order. Inorder(root) { if (!root) { return ; } this .Inorder(root.left); print( root.data ); this .Inorder(root.right); } }; // convert array to decimal function convert(arr) { var sum=0; for ( var i=0; i<COL; i++) { sum+=Math.pow(2,i)*arr[i]; } return sum; } // print the column represented as integers function print(p) { for ( var i=0; i<COL; i++) { document.write(p%2 + " " ); p=parseInt(p/2); } document.write( "<br>" ); } // The main function that prints // all unique rows in a given matrix. function findUniqueRows(M) { var b = new BST(0),root = null ; //Traverse through the matrix for ( var i=0; i<ROW; i++) { //insert the row into BST root=b.Insert(root,convert(M[i])); } //print b.Inorder(root); } // Driver Code var M = [[0, 1, 0, 0, 1], [1, 0, 1, 1, 0], [0, 1, 0, 0, 1], [1, 0, 1, 0, 0]]; findUniqueRows(M); // This code is contributed by rutvik_56. </script> |
输出:
1 0 1 0 0 1 0 1 1 0 0 1 0 0 1
复杂性分析:
- 时间复杂性: O(第x列+第x列日志(第行))。 遍历矩阵的时间复杂度为O(ROW x COL),并将它们插入BST,每行的时间复杂度为O(log ROW)。所以总体时间复杂度是O(ROW x COL+ROW x log(ROW))
- 辅助空间: O(世界其他地区)。 要存储BST,需要O(行)空间。
方法3 : 这种方法使用 Trie数据结构 解决上述问题。Trie是一种高效的信息检索数据结构。使用Trie,搜索复杂度可以达到最佳极限(密钥长度)。如果我们将密钥存储在二叉搜索树中,一个平衡良好的BST将需要与M*logn成比例的时间,其中M是最大字符串长度,N是树中的密钥数。使用Trie,我们可以在O(M)时间内搜索密钥。然而,惩罚取决于Trie的存储要求。 注: 如果列数较大,此方法将导致整数溢出。
方法: 由于矩阵是布尔型的,因此可以使用Trie数据结构的一种变体,其中每个节点将有两个子节点,一个表示0,另一个表示1。在Trie中插入每一行。如果该行已经存在,则不要打印该行。如果Trie中没有该行,请将其插入Trie并打印。
算法:
- 创建一个可以存储行的Trie。
- 遍历矩阵并将行插入Trie。
- Trie无法存储重复条目,因此将删除重复条目
- 遍历Trie并打印行。
实施:
C++
// Given a binary matrix of M X N of integers, // you need to return only unique rows of binary array #include <bits/stdc++.h> using namespace std; #define ROW 4 #define COL 5 // A Trie node class Node { public : bool isEndOfCol; Node *child[2]; // Only two children needed for 0 and 1 } ; // A utility function to allocate memory // for a new Trie node Node* newNode() { Node* temp = new Node(); temp->isEndOfCol = 0; temp->child[0] = temp->child[1] = NULL; return temp; } // Inserts a new matrix row to Trie. // If row is already present, // then returns 0, otherwise insets the row and // return 1 bool insert(Node** root, int (*M)[COL], int row, int col ) { // base case if (*root == NULL) *root = newNode(); // Recur if there are more entries in this row if (col < COL) return insert (&((*root)->child[M[row][col]]), M, row, col + 1); else // If all entries of this row are processed { // unique row found, return 1 if (!((*root)->isEndOfCol)) return (*root)->isEndOfCol = 1; // duplicate row found, return 0 return 0; } } // A utility function to print a row void printRow( int (*M)[COL], int row) { int i; for (i = 0; i < COL; ++i) cout << M[row][i] << " " ; cout << endl; } // The main function that prints // all unique rows in a given matrix. void findUniqueRows( int (*M)[COL]) { Node* root = NULL; // create an empty Trie int i; // Iterate through all rows for (i = 0; i < ROW; ++i) // insert row to TRIE if (insert(&root, M, i, 0)) // unique row found, print it printRow(M, i); } // Driver Code int main() { int M[ROW][COL] = {{0, 1, 0, 0, 1}, {1, 0, 1, 1, 0}, {0, 1, 0, 0, 1}, {1, 0, 1, 0, 0}}; findUniqueRows(M); return 0; } // This code is contributed by rathbhupendra |
C
//Given a binary matrix of M X N of integers, you need to return only unique rows of binary array #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define ROW 4 #define COL 5 // A Trie node typedef struct Node { bool isEndOfCol; struct Node *child[2]; // Only two children needed for 0 and 1 } Node; // A utility function to allocate memory for a new Trie node Node* newNode() { Node* temp = (Node *) malloc ( sizeof ( Node ) ); temp->isEndOfCol = 0; temp->child[0] = temp->child[1] = NULL; return temp; } // Inserts a new matrix row to Trie. If row is already // present, then returns 0, otherwise insets the row and // return 1 bool insert( Node** root, int (*M)[COL], int row, int col ) { // base case if ( *root == NULL ) *root = newNode(); // Recur if there are more entries in this row if ( col < COL ) return insert ( &( (*root)->child[ M[row][col] ] ), M, row, col+1 ); else // If all entries of this row are processed { // unique row found, return 1 if ( !( (*root)->isEndOfCol ) ) return (*root)->isEndOfCol = 1; // duplicate row found, return 0 return 0; } } // A utility function to print a row void printRow( int (*M)[COL], int row ) { int i; for ( i = 0; i < COL; ++i ) printf ( "%d " , M[row][i] ); printf ( "" ); } // The main function that prints all unique rows in a // given matrix. void findUniqueRows( int (*M)[COL] ) { Node* root = NULL; // create an empty Trie int i; // Iterate through all rows for ( i = 0; i < ROW; ++i ) // insert row to TRIE if ( insert(&root, M, i, 0) ) // unique row found, print it printRow( M, i ); } // Driver program to test above functions int main() { int M[ROW][COL] = {{0, 1, 0, 0, 1}, {1, 0, 1, 1, 0}, {0, 1, 0, 0, 1}, {1, 0, 1, 0, 0} }; findUniqueRows( M ); return 0; } |
输出:
0 1 0 0 1 1 0 1 1 0 1 0 1 0 0
复杂性分析:
- 时间复杂性: O(第x列)。 要遍历矩阵并插入trie,时间复杂度为O(ROW x COL)。该方法具有较好的时间复杂度。此外,打印时保持行的相对顺序,但会占用空间。
- 辅助空间: O(第x列)。 存储Trie O(ROW x COL)需要复杂的空间。
方法4 : 这种方法使用 哈希集数据结构 解决上述问题。HashSet类实现Set接口,该接口由一个哈希表支持,该哈希表实际上是一个HashMap实例。不保证集合的迭代顺序,这意味着该类不保证元素随时间的恒定顺序。此类允许空元素。假设哈希函数将元素正确地分散在存储桶中,该类为添加、移除、包含和大小等基本操作提供了恒定的时间性能。
方法: 在这个方法中,将整行转换为单个字符串,然后检查它是否已经存在于哈希集中。如果该行存在,那么我们将保留它,否则我们将打印唯一的行并将其添加到HashSet。
算法:
- 创建一个哈希集,其中行可以存储为字符串。
- 遍历矩阵并将行作为字符串插入哈希集中。
- HashSet无法存储重复项,因此将删除重复项
- 遍历哈希集并打印行。
实施:
C++
// C++ code to print unique row in a // given binary matrix #include<bits/stdc++.h> using namespace std; void printArray( int arr[][5], int row, int col) { unordered_set<string> uset; for ( int i = 0; i < row; i++) { string s = "" ; for ( int j = 0; j < col; j++) s += to_string(arr[i][j]); if (uset.count(s) == 0) { uset.insert(s); cout << s << endl; } } } // Driver code int main() { int arr[][5] = {{0, 1, 0, 0, 1}, {1, 0, 1, 1, 0}, {0, 1, 0, 0, 1}, {1, 1, 1, 0, 0}}; printArray(arr, 4, 5); } // This code is contributed by // rathbhupendra |
JAVA
// Java code to print unique row in a // given binary matrix import java.util.HashSet; public class GFG { public static void printArray( int arr[][], int row, int col) { HashSet<String> set = new HashSet<String>(); for ( int i = 0 ; i < row; i++) { String s = "" ; for ( int j = 0 ; j < col; j++) s += String.valueOf(arr[i][j]); if (!set.contains(s)) { set.add(s); System.out.println(s); } } } // Driver code public static void main(String[] args) { int arr[][] = { { 0 , 1 , 0 , 0 , 1 }, { 1 , 0 , 1 , 1 , 0 }, { 0 , 1 , 0 , 0 , 1 }, { 1 , 1 , 1 , 0 , 0 } }; printArray(arr, 4 , 5 ); } } |
Python3
# Python3 code to print unique row in a # given binary matrix def printArray(matrix): rowCount = len (matrix) if rowCount = = 0 : return columnCount = len (matrix[ 0 ]) if columnCount = = 0 : return row_output_format = " " .join([ "%s" ] * columnCount) printed = {} for row in matrix: routput = row_output_format % tuple (row) if routput not in printed: printed[routput] = True print (routput) # Driver Code mat = [[ 0 , 1 , 0 , 0 , 1 ], [ 1 , 0 , 1 , 1 , 0 ], [ 0 , 1 , 0 , 0 , 1 ], [ 1 , 1 , 1 , 0 , 0 ]] printArray(mat) # This code is contributed by myronwalker |
C#
using System; using System.Collections.Generic; // c# code to print unique row in a // given binary matrix public class GFG { public static void printArray( int [][] arr, int row, int col) { HashSet< string > set = new HashSet< string >(); for ( int i = 0; i < row; i++) { string s = "" ; for ( int j = 0; j < col; j++) { s += arr[i][j].ToString(); } if (! set .Contains(s)) { set .Add(s); Console.WriteLine(s); } } } // Driver code public static void Main( string [] args) { int [][] arr = new int [][] { new int [] {0, 1, 0, 0, 1}, new int [] {1, 0, 1, 1, 0}, new int [] {0, 1, 0, 0, 1}, new int [] {1, 1, 1, 0, 0} }; printArray(arr, 4, 5); } } // This code is contributed by Shrikant13 |
Javascript
<script> // Javascript code to print unique row in a // given binary matrix function printArray(arr,row,col) { let set = new Set(); for (let i = 0; i < row; i++) { let s = "" ; for (let j = 0; j < col; j++) s += (arr[i][j]).toString(); if (!set.has(s)) { set.add(s); document.write(s+ "<br>" ); } } } // Driver code let arr = [[0, 1, 0, 0, 1], [1, 0, 1, 1, 0], [0, 1, 0, 0, 1], [1, 1, 1, 0, 0]]; printArray(arr, 4, 5); // This code is contributed by avanitrachhadiya2155 </script> |
输出:
010011011011100
复杂性分析:
- 时间复杂性: O(第x列)。 要遍历矩阵并插入哈希集,时间复杂度为O(行x列)
- 辅助空间: O(世界其他地区)。 要存储HashSet O(ROW x COL),需要复杂的空间。
谢谢Anshuman Kaushik提出这个方法。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。