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

发布于 2024-05-20 07:39:30 字数 9284 浏览 23 评论 0

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

Input: arr[][] ={ { 1, 1, 0, 1 }, { 0, 0, 0, 0 }, { 1, 1, 0, 1}, { 0, 1, 0, 1 } } 
Output:
111
111
011
Explanation:
Initially, the matrix is as follows:
arr[][] = { { 1, 1, 0, 1 },
{ 0, 0, 0, 0 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 1 } }
Removing the 2nd row modifies the matrix to:
arr[][] = { { 1, 1, 0, 1 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 1 } }
Removing the 3rd column modifies the matrix to:
arr[][] = { { 1, 1, 1 },
{ 1, 1, 1 },
{ 0, 1, 1 } }
Input: arr={{0, 1}, {0, 1}} 
Output:
1
1

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

  • 遍历矩阵并在行和列中计数 1s。
  • 现在,再次遍历循环并检查以下内容:
    • 如果发现任何行的 1s 计数为 0 ,则跳过该行。
    • 如果发现任何列的 1s 计数大于 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<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<vector > arr = { { 1, 1, 0, 1 },   
                                 { 0, 0, 0, 0 },   
                                 { 1, 1, 0, 1 },   
                                 { 0, 1, 0, 1 } };   
      
    // Function Call   
    removeZeroRowCol(arr);   
    return 0;   
}</vector</vector

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     )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

童话

暂无简介

文章
评论
28 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文