Java 中查詢二進位制矩陣中含零最多的列


在 Java 中,陣列是一個物件。它是一種非基本資料型別,用於儲存相同資料型別的多個值。Java 中的矩陣只不過是一個多維陣列,它表示多行多列。

二進位制矩陣是由 0 和 1 組成的矩陣,其中每個元素只能是兩個可能值中的一個。

這裡我們給出一個包含二進位制元素的二進位制矩陣,根據題意,我們需要找到包含最多 0 的列。

讓我們開始吧!

舉幾個例子

示例 1

給定二進位制矩陣 =

1 1 0 0
0 1 0 1
0 0 1 1
1 1 1 1
  • 包含最多零的列是:1 和 3

示例 2

給定二進位制矩陣 =

0 0 1 1 1
1 0 1 0 0
0 0 0 1 1
0 1 1 1 0
0 1 0 0 1
  • 包含最多零的列是:1

示例 3

給定二進位制矩陣 =

1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
  • 包含最多零的列是:不存在這樣的列

演算法

演算法 1:(使用巢狀 for 迴圈)

  • 步驟 1 − 程式初始化一個二進位制矩陣和兩個變數,用於儲存最大零計數及其對應的列索引。

  • 步驟 2 − 然後迴圈遍歷矩陣的每一列,並計算該列中零的數量。

  • 步驟 3 − 如果當前列的零的數量更多,則更新最大零計數及其對應的列索引。

  • 步驟 4 − 最後,列印包含最多零的列,或者如果矩陣中沒有零,則列印“給定矩陣不包含任何零”。

演算法 2:(使用 Java 流)

  • 步驟 1 − 程式初始化一個二進位制矩陣和兩個變數,用於儲存最大零計數及其對應的列索引。

  • 步驟 2 − 然後迴圈遍歷矩陣的每一列,並使用 Java 流來計算該列中零的數量。

  • 步驟 3 − 如果當前列的零的數量更多,則更新最大零計數及其對應的列索引。

  • 步驟 4 − 最後,列印包含最多零的列,或者如果矩陣中沒有零,則列印“給定矩陣不包含任何零”。

  • 步驟 5 − Java 流提供了一種用於處理資料的函數語言程式設計方法,可以顯著減少執行集合資料上的複雜操作所需的程式碼量。

語法

Java 中的Matrix.length()方法返回給定矩陣的長度。

以下是其語法:

inputMatrix.lenght

其中,'inputMatrix' 指的是給定的矩陣。

Arrays.stream(matrix) 將二維整數陣列矩陣轉換為陣列流,其中流中的每個陣列表示矩陣中的一行。

Arrays.stream(matrix)

.mapToInt(row -> row[column]) 將流中的每個陣列(行)對映到與指定列索引處的元素對應的整數值。

.mapToInt(row -> row[column]) 

多種方法

我們提供了不同的方法來解決這個問題。

  • 使用巢狀 for 迴圈

  • 使用 Java 流

讓我們一一檢視程式及其輸出。

方法 1:使用巢狀 for 迴圈

在這種方法中,二進位制矩陣元素將在程式中初始化。然後呼叫一個使用者定義的方法,並將矩陣作為引數傳遞,在方法內部,根據演算法 1 使用巢狀 for 迴圈計算給定二進位制矩陣中包含最多 0 的列。

示例

public class Main {
   public static void main(String[] args) {
      
      // Create a binary matrix
      int[][] matrix = {{1, 0, 1, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}, {0, 1, 1, 0}};
      
      // Initialize variables to store the maximum zero count and its corresponding column index
      int maxZeroCount = -1;
      int maxZeroColumn = -1;
      
      // Loop through each column of the matrix and count the number of zeros in that column
      for (int j = 0; j < matrix[0].length; j++) {
         int zeroCount = 0;
         for (int[] row : matrix) {
            if (row[j] == 0) {
               zeroCount++;
            }
         }
         
         // Update the maximum zero count and its corresponding column index if the current column has more zeros
         if (zeroCount > maxZeroCount) {
            maxZeroCount = zeroCount;
            maxZeroColumn = j;
         }
      }
      
      // Print the result
      if (maxZeroCount == 0) {
         System.out.println("The given matrix does not contain any zeros.");
      } else {
         System.out.print("The column(s) with the maximum number of zeros is/are: ");
         for (int j = 0; j < matrix[0].length; j++) {
            int zeroCount = 0;
            for (int[] row : matrix) {
               if (row[j] == 0) {
                  zeroCount++;
               }
            }
            if (zeroCount == maxZeroCount) {
               System.out.print(j + " ");
            }
         }
      }
   }
}

輸出

The column(s) with the maximum number of zeros is/are: 3

方法 2:使用 Java 流

在這種方法中,二進位制矩陣元素將在程式中初始化。然後呼叫一個使用者定義的方法,並將矩陣作為引數傳遞,在方法內部,根據演算法 2 使用 Java 流計算給定二進位制矩陣中包含最多 0 的列。

示例

import java.util.Arrays;
public class Main {
   public static void main(String[] args) {
      
      // Create a binary matrix
      int[][] matrix = {{1, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}, {0, 1, 1, 0}};
      
      // Initialize variables to store the maximum zero count and its corresponding column index
      int maxZeroCount = -1;
      int maxZeroColumn = -1;
      
      // Loop through each column of the matrix and count the number of zeros in that column using a lambda expression
      for (int j = 0; j < matrix[0].length; j++) {
         final int column = j; // Make a copy of j to use inside the lambda expression
         int zeroCount = (int) Arrays.stream(matrix)
               .mapToInt(row -> row[column])
               .filter(value -> value == 0)
               .count();
      
         // Update the maximum zero count and its corresponding column index if the current column has more zeros
         if (zeroCount > maxZeroCount) {
            maxZeroCount = zeroCount;
            maxZeroColumn = j;
         }
      }
      
      // Print the result
      if (maxZeroCount == 0) {
         System.out.println("The given matrix does not contain any zeros.");
      } else {
         System.out.print("The column(s) with the maximum number of zeros is/are: ");
         for (int j = 0; j < matrix[0].length; j++) {
            final int column = j; // Make a copy of j to use inside the lambda expression
            int zeroCount = (int) Arrays.stream(matrix)
               .mapToInt(row -> row[column])
               .filter(value -> value == 0)
               .count();
            if (zeroCount == maxZeroCount) {
               System.out.print(j + " ");
            }
         }
      }
   }
}

輸出

The column(s) with the maximum number of zeros is/are: 2 3

在本文中,我們探討了使用 Java 程式語言查詢二進位制矩陣中包含最多 0 的列的不同方法

更新於:2023 年 5 月 4 日

瀏覽量 246 次

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.