Java - 如果包含目标编号,则将2d数组的行/列清零

时间:2022-07-08 21:31:25

Given a mxn matrix that looks like this:
1034
1234
3560

给定mxn矩阵,如下所示:1034 1234 3560

Need to output something like this:
0000
1030
0000

需要输出这样的东西:0000 1030 0000

*Target number is 0.

*目标编号为0。

Here's my solution, but I think it's not very efficient (both space and running time which I believe is O(m^2 * n)) and was wondering if there are simpler and more efficient ways to do this. If yes, what is it?

这是我的解决方案,但我认为它不是非常有效(我认为空间和运行时间都是O(m ^ 2 * n))并且想知道是否有更简单和更有效的方法来做到这一点。如果是,那是什么?

int[][] m = { { 1, 0, 3, 4 }, { 1, 2, 3, 4 }, { 3, 5, 6, 0 } };  
m = zero(m, 0);

public static int[][] zero(int[][] m, int num) {

    int rows = m.length;
    int columns = m[0].length;
    int [][] myInt = new int[rows][];
    for(int i = 0; i < rows; i++){
        myInt[i] = m[i].clone();
    }
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            if (myInt[i][j] == num) {
                m[i] = new int[columns];
                for(int k = 0; k < rows; k++){
                    m[k][j] = 0;
                }
                break;
            }
        }
    }       
    return m;
}

Basically I clone the input matrix first, then iterate through each row and check if the row contains my target number. If yes, then I set the entire row in the original matrix to zero. Then I do another loop to set the column which contains the target number to zero. I cloned the matrix in the beginning so the checking is always against the cloned reference matrix instead of the modified one at each iteration.

基本上我首先克隆输入矩阵,然后遍历每一行并检查该行是否包含我的目标数字。如果是,那么我将原始矩阵中的整行设置为零。然后我再做一个循环,将包含目标数的列设置为零。我在开始时克隆了矩阵,因此检查总是针对克隆的参考矩阵,而不是每次迭代时修改的参考矩阵。

2 个解决方案

#1


1  

I propose using BitSet for row/column indices.

我建议将BitSet用于行/列索引。

public static void zero(int[][] m, int num) {

    int rows = m.length;
    int columns = m[0].length;
    BitSet rowsToClear = new BitSet(rows);
    BitSet columnsToClear = new BitSet(columns);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            if (m[i][j] == num) {
                rowsToClear.set(i);
                columnsToClear.set(j);
            }
        }
    }
    for (int i = rowsToClear.nextSetBit(0); i >= 0;
             i = rowsToClear.nextSetBit(i + 1)) {
        Arrays.fill(m[i], 0);
    }
    for (int j = columnsToClear.nextSetBit(0); j >= 0;
            j = columnsToClear.nextSetBit(j + 1)) {
        for (int i = 0; i < rows; ++i) {
            m[i][j] = 0;
        }
    }
    //return m;
}

#2


1  

I just came across this question and have developed a solution for it.

我刚刚遇到了这个问题,并为此开发了一个解决方案。

I am hoping for some feedback regarding the code about how it's better/worse and it's runtime and space complexity. Pretty new to all this :)

我希望得到关于代码的一些反馈,关于它如何变得更好/更糟,以及它的运行时和空间复杂性。对这一切都很新:)

public static void zeroMatrix(int[][] arr1)
{
    ArrayList<Integer> coord = new ArrayList<>();

    int row = arr1.length; 
    int column = arr1[0].length;
    for(int i=0; i < row; i++)
    {
        for(int j=0; j < column; j++)
        {
            if(arr1[i][j]==0)
            { 
                coord.add((10*i) + j);
            }       
        }
    }

    for(int n : coord)
    {
         int j=n%10;
         int i=n/10; int k=0;
        int l=0;
        while(k<row)
        {
            arr1[k][j]=0;
            k++;
        }
        while(l<column)
        {
            arr1[i][l]=0;
            l++;
        }
    }
}

#1


1  

I propose using BitSet for row/column indices.

我建议将BitSet用于行/列索引。

public static void zero(int[][] m, int num) {

    int rows = m.length;
    int columns = m[0].length;
    BitSet rowsToClear = new BitSet(rows);
    BitSet columnsToClear = new BitSet(columns);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            if (m[i][j] == num) {
                rowsToClear.set(i);
                columnsToClear.set(j);
            }
        }
    }
    for (int i = rowsToClear.nextSetBit(0); i >= 0;
             i = rowsToClear.nextSetBit(i + 1)) {
        Arrays.fill(m[i], 0);
    }
    for (int j = columnsToClear.nextSetBit(0); j >= 0;
            j = columnsToClear.nextSetBit(j + 1)) {
        for (int i = 0; i < rows; ++i) {
            m[i][j] = 0;
        }
    }
    //return m;
}

#2


1  

I just came across this question and have developed a solution for it.

我刚刚遇到了这个问题,并为此开发了一个解决方案。

I am hoping for some feedback regarding the code about how it's better/worse and it's runtime and space complexity. Pretty new to all this :)

我希望得到关于代码的一些反馈,关于它如何变得更好/更糟,以及它的运行时和空间复杂性。对这一切都很新:)

public static void zeroMatrix(int[][] arr1)
{
    ArrayList<Integer> coord = new ArrayList<>();

    int row = arr1.length; 
    int column = arr1[0].length;
    for(int i=0; i < row; i++)
    {
        for(int j=0; j < column; j++)
        {
            if(arr1[i][j]==0)
            { 
                coord.add((10*i) + j);
            }       
        }
    }

    for(int n : coord)
    {
         int j=n%10;
         int i=n/10; int k=0;
        int l=0;
        while(k<row)
        {
            arr1[k][j]=0;
            k++;
        }
        while(l<column)
        {
            arr1[i][l]=0;
            l++;
        }
    }
}