📜  从矩阵中删除所有零行和所有零列

📅  最后修改于: 2021-04-29 05:39:20             🧑  作者: Mango

给定大小为N * M的矩阵arr [] [] ,任务是在从矩阵中删除仅由0 s组成的所有行和列之后,打印矩阵。

例子:

方法:想法是对矩阵的所有行和列中的0 s进行计数,并检查是否有任何行或列仅由0 s组成。如果发现为真,则删除矩阵的那些行或列。请按照以下步骤解决问题:

  • 遍历矩阵并在行和列中计数1 s。
  • 现在,再次遍历循环并检查以下内容:
    • 如果发现任一行的1 s计数为0 ,请跳过该行。
    • 如果发现任何列的1 s计数都大于0 ,则打印该元素。
C++
// C++ program for the above approach   
#include    
using namespace std;   
      
// Function to remove the rows or columns from   
// the matrix which contains all 0s elements   
void removeZeroRowCol(vector >& arr)   
{   
      
    // Stores count of rows   
    int n = arr.size();   
      
    // col[i]: Stores count of 0s   
    // in current column   
    int col[n + 1] = { 0 };   
      
    // row[i]: Stores count of 0s   
    // in current row   
    int row[n + 1] = { 0 };   
      
    // Traverse the matrix   
    for (int i = 0; i < n; ++i) {   
      
        // Stores count of 0s   
        // in current row   
        int count = 0;   
      
        for (int j = 0; j < n; ++j) {   
      
            // Update col[j]   
            col[j] += (arr[i][j] == 1);   
      
            // Update count   
            count += (arr[i][j] == 1);   
        }   
      
        // Update row[i]   
        row[i] = count;   
    }   
      
    // Traverse the matrix   
    for (int i = 0; i < n; ++i) {   
      
        // If all elements of   
        // current row is 0   
        if (row[i] == 0) {   
            continue;   
        }   
        for (int j = 0; j < n; ++j) {   
      
            // If all elements of   
            // current column is 0   
            if (col[j] != 0)   
                cout << arr[i][j];   
        }   
        cout << "\n";   
    }   
}   
      
// Driver Code   
int main()   
{   
    vector > arr = { { 1, 1, 0, 1 },   
                                 { 0, 0, 0, 0 },   
                                 { 1, 1, 0, 1 },   
                                 { 0, 1, 0, 1 } };   
      
    // Function Call   
    removeZeroRowCol(arr);   
    return 0;   
}


Java
// Java program for the above approach   
class GFG{
         
// Function to remove the rows or columns from   
// the matrix which contains all 0s elements   
static void removeZeroRowCol(int arr[][])   
{   
     
    // Stores count of rows   
    int n = arr.length;   
      
    // col[i]: Stores count of 0s   
    // in current column   
    int col[] = new int[n + 1];   
      
    // row[i]: Stores count of 0s   
    // in current row   
    int row[] = new int[n + 1];   
      
    // Traverse the matrix   
    for(int i = 0; i < n; ++i)
    {   
         
        // Stores count of 0s   
        // in current row   
        int count = 0;   
      
        for(int j = 0; j < n; ++j)
        {
            if (arr[i][j] == 1)
             
                // Update col[j]   
                col[j] += 1;   
            else
                col[j] += 0;
      
            if (arr[i][j] == 1)
             
                // Update count       
                count += 1;   
            else
                count += 0;
        }   
      
        // Update row[i]   
        row[i] = count;   
    }   
      
    // Traverse the matrix   
    for(int i = 0; i < n; ++i)
    {   
         
        // If all elements of   
        // current row is 0   
        if (row[i] == 0)
        {   
            continue;   
        }   
        for(int j = 0; j < n; ++j)
        {   
             
            // If all elements of   
            // current column is 0   
            if (col[j] != 0)   
                System.out.print(arr[i][j]);   
        }   
        System.out.println();   
    }   
}   
 
// Driver Code   
public static void main (String[] args)   
{   
    int arr[][] = { { 1, 1, 0, 1 },   
                    { 0, 0, 0, 0 },   
                    { 1, 1, 0, 1 },   
                    { 0, 1, 0, 1 } };   
      
    // Function Call   
    removeZeroRowCol(arr);   
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach    
      
# Function to remove the rows or columns from    
# the matrix which contains all 0s elements    
def removeZeroRowCol(arr) :    
       
    # Stores count of rows    
    n = len(arr)    
       
    # col[i]: Stores count of 0s    
    # in current column    
    col = [0] * (n + 1)    
       
    # row[i]: Stores count of 0s    
    # in current row    
    row = [0] * (n + 1)    
       
    # Traverse the matrix    
    for i in range(n) :   
       
        # Stores count of 0s    
        # in current row    
        count = 0   
       
        for j in range(n) :   
       
            # Update col[j]    
            col[j] += (arr[i][j] == 1)   
       
            # Update count    
            count += (arr[i][j] == 1)   
       
        # Update row[i]    
        row[i] = count   
       
    # Traverse the matrix    
    for i in range(n) :   
       
        # If all elements of    
        # current row is 0    
        if (row[i] == 0) :   
            continue   
             
        for j in range(n) :   
       
            # If all elements of    
            # current column is 0    
            if (col[j] != 0) :   
                print(arr[i][j], end = "")    
             
        print()   
             
arr = [ [ 1, 1, 0, 1 ],    
         [ 0, 0, 0, 0 ],    
         [ 1, 1, 0, 1 ],    
         [ 0, 1, 0, 1 ] ]   
       
# Function Call    
removeZeroRowCol(arr)
 
# This code is contributed by divyeshrabadiya07


C#
// C# program for the above approach   
using System;
 
class GFG{
         
// Function to remove the rows or columns from   
// the matrix which contains all 0s elements   
static void removeZeroRowCol(int[,] arr)   
{   
     
    // Stores count of rows   
    int n = arr.GetLength(0);   
      
    // col[i]: Stores count of 0s   
    // in current column   
    int[] col = new int[n + 1];   
      
    // row[i]: Stores count of 0s   
    // in current row   
    int[] row = new int[n + 1];   
      
    // Traverse the matrix   
    for(int i = 0; i < n ; ++i)
    {   
         
        // Stores count of 0s   
        // in current row   
        int count = 0;   
      
        for(int j = 0; j < n ; ++j)
        {
            if (arr[i, j] == 1)
             
                // Update col[j]   
                col[j] += 1;   
            else
                col[j] += 0;
      
            if (arr[i, j] == 1)
             
                // Update count       
                count += 1;   
            else
                count += 0;
        }   
      
        // Update row[i]   
        row[i] = count;   
    }   
      
    // Traverse the matrix   
    for(int i = 0; i < n; ++i)
    {   
         
        // If all elements of   
        // current row is 0   
        if (row[i] == 0)
        {   
            continue;   
        }   
        for(int j = 0; j < n; ++j)
        {   
             
            // If all elements of   
            // current column is 0   
            if (col[j] != 0)   
                Console.Write(arr[i, j]);   
        }   
        Console.WriteLine();   
    }   
}   
 
// Driver Code   
public static void Main (String[] args)   
{   
    int[,] arr = { { 1, 1, 0, 1 },   
                   { 0, 0, 0, 0 },   
                   { 1, 1, 0, 1 },   
                   { 0, 1, 0, 1 } };   
      
    // Function Call   
    removeZeroRowCol(arr);   
}
}
 
// This code is contributed by susmitakundugoaldanga


输出 :

111
111
011

时间复杂度: O( n^2   )
空间复杂度: O( n*m   )