Java 找出二进制矩阵中0最多的列
在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流为处理数据提供了一种函数式编程方法,并且可以显著减少执行对数据集进行复杂操作所需的代码量。
语法
Matrix.length() 方法在Java中返回给定矩阵的长度。
下面是它的语法:
inputMatrix.lenght
其中,’inputMatrix’指的是给定的矩阵。
Arrays.stream(matrix) 将二维整数数组矩阵转换为一个数组流,其中流中的每个数组表示矩阵中的一行。
Arrays.stream(matrix)
.count() 将流中的每个数组(行)映射为对应于指定列索引处元素的整数值。
.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 Stream
在这种方法中,程序将初始化二进制矩阵元素。然后调用一个用户定义的方法,通过将矩阵作为参数传递,并在方法内部按照算法2使用Java Stream计算给定二进制矩阵中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最多的列。