Dada una array binaria, imprima todas las filas únicas de la array dada.
Ejemplo:
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>
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
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 # print the column represented as integers def Print(p): for i in range(COL): print(p % 2 ,end = " ") p = int(p//2) print("") class BST: def __init__(self,data): self.data = data self.left = None self.right = None # Insert function definition. def Insert(self,root, value): if(not root): # Insert the first node, if root is NULL. return 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 = self.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 = self.Insert(root.left, value) # Return 'root' node, after insertion. return root # Inorder traversal function. # This gives data in sorted order. def Inorder(self,root): if(not root): return self.Inorder(root.left); Print( root.data ); self.Inorder(root.right) # convert array to decimal def convert(arr): sum=0 for i in range(COL): sum+=pow(2,i)*arr[i] return sum # The main function that prints # all unique rows in a given matrix. def findUniqueRows(M): b,root =BST(0),None #Traverse through the matrix for i in range(ROW): #insert the row into BST root = b.Insert(root,convert(M[i])) #print b.Inorder(root) # Driver Code 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 shinjanpatra
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>
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("\n"); } // 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; }
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>
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA