Java 修改矩阵并将列和行设置为零的算法

Java 修改矩阵并将列和行设置为零的算法,java,arrays,algorithm,matrix,time-complexity,Java,Arrays,Algorithm,Matrix,Time Complexity,这在技术上是一个代码挑战。 在一次采访中,我被问到了一个有趣的问题,我希望能有一些见解,因为我能给出的最好答案是O(2n^2)-n平方范畴,但仍然是蛮力 假设有一个大小为M×N的矩阵(一个数组数组(int[][])) 如果单元格包含零,则将整行和整列设置为零。 产生结果: 0 2 0 3 1 0 0 0 0 0 0 8 0 2 8 0 0 0 0 0 最快和/或最好的方法是什么 我自己的答案是迭代整个数组,跟踪行和列,使其归零,然后再归零 public void zeroOut(int[

这在技术上是一个代码挑战。 在一次采访中,我被问到了一个有趣的问题,我希望能有一些见解,因为我能给出的最好答案是O(2n^2)-n平方范畴,但仍然是蛮力

假设有一个大小为M×N的矩阵(一个数组数组(
int[][]
))

如果单元格包含零,则将整行和整列设置为零。
产生结果:

0 2 0 3 1
0 0 0 0 0 
0 8 0 2 8
0 0 0 0 0 
最快和/或最好的方法是什么


我自己的答案是迭代整个数组,跟踪行和列,使其归零,然后再归零

public void zeroOut(int[][] myArray){
    ArrayList<Integer> rowsToZero = new....
    ArrayList<Integer> columnsToZero = new....

    for(int i=0; i<myArray.length; i++){ // record which rows and columns will be zeroed
        for(int j=0; j<myArray[i].length; i++){
            if(myArray[i][j] == 0){
                if(!rowsToZero.contains(i)) rowsToZero.add(i);
                if(!columnsToZero.contains(j)) columnsToZero.add(j);
            }
        }
    }
    for(int row : rows){ // now zero the rows
        myArray[row] = int[myArray.length];
    }

    for(int i=0; i<myArray.length; i++){
        for(int column: columns){ // now zero the columns
            myArray[i][column] = 0;
        }    
    }
}
publicsvoidzeroout(int[][]myArray){
ArrayList rowsToZero=新建。。。。
ArrayList ColumnStorzero=新建。。。。

对于(int i=0;i可以通过取两个
int
来实现这一点,但唯一的条件是行数和列数应小于或等于32。对于大于32的行数和列数,也可以这样做,但必须取整数数组

因此,逻辑是:

  • 取两个整数,即
  • 如果
    矩阵[i][j]=0,则遍历矩阵,然后在
    行和
    列中设置相应的位
  • 在遍历之后,如果设置了
    的相应位,则再次遍历并设置
    矩阵[i][j]=0
时间复杂度与O(N^2)
相同,但内存效率较高。请在下面查找我的代码


检查
数组[row][col]==0
如果为0,则是否设置了
r
c
中的相应位

        int r = 0, c = 0;
        for (int row = 0; row < 5; row++) {
            for (int col = 0; col < 7; col++) {
                if (array[row][col] == 0) {
                    r = r | (1<<row);
                    c = c | (1<<col);
                }
            }
        }
intr=0,c=0;
对于(int行=0;行<5;行++){
for(int col=0;col<7;col++){
if(数组[行][列]==0){

r=r |(1将矩阵分成相等的更小的矩阵部分,然后计算确定量,这样你就可以预测这个矩阵部分中有一个零。 然后只使用蛮力机制对这个预选矩阵进行确定 在行或列中,零为零

行列式只是一个建议,也许你可以使用其他类型的线性代数 预测零值的算法和规则

更新:

如果您使用快速排序概念来组织每一行的时间,那么您只需循环,直到出现第一个none-zero元素。 在排序过程中,您需要记住哪个列索引和0关联

意味着

 1 2 6 0 3
库奇克索特(

当您记住列索引时,您现在可以直接知道用0填充哪一行和哪一列

快速排序列表的平均值O(n日志n)最坏情况n*n


也许这已经改善了整体的复杂性。

到目前为止,似乎还没有人真正想出一个更快/更好的算法,所以这一个似乎就是它。谢谢大家的投入

public void zeroOut(int[][] myArray){
    ArrayList<Integer> rowsToZero = new....
    ArrayList<Integer> columnsToZero = new....

    for(int i=0; i<myArray.length; i++){ // record which rows and columns will be zeroed
        for(int j=0; j<myArray[i].length; i++){
            if(myArray[i][j] == 0){
                if(!rowsToZero.contains(i)) rowsToZero.add(i);
                if(!columnsToZero.contains(j)) columnsToZero.add(j);
            }
        }
    }
    for(int row : rows){ // now zero the rows
        myArray[row] = int[myArray.length];
    }

    for(int i=0; i<myArray.length; i++){
        for(int column: columns){ // now zero the columns
            myArray[i][column] = 0;
        }    
    }
}
publicsvoidzeroout(int[][]myArray){
ArrayList rowsToZero=新建。。。。
ArrayList ColumnStorzero=新建。。。。

对于(inti=0;i我刚刚遇到了这个问题,并为它开发了一个解决方案。 我希望我能得到一些关于代码的反馈,关于它的好坏以及它的运行时间。 这一切都很新鲜:)

publicstaticvoidzeromatrix(int[]arr1)
{
ArrayList coord=新的ArrayList();
int row=arr1.1长度;
int column=arr1[0]。长度;
对于(int i=0;i而(k我使用了HashMap。看看这是否有任何帮助

import java.util.*;
class ZeroMatrix
{
public static void main(String args[])
{
int mat[][]=new int[][]{{0,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,0}};
HashMap<Integer,Integer> ht=new HashMap<Integer,Integer>();
for(int i=0;i<5;i++)
{
for(int j=0;j<4;j++)
{
if(mat[i][j]==0)
ht.put(i,j);
}
}

//Set the Respected Rows and colums to Zeros 
Set set=ht.entrySet();
Iterator itr=set.iterator();
while(itr.hasNext())
{
Map.Entry m=(Map.Entry)itr.next();
int i=(Integer)m.getKey();
int k=(Integer)m.getValue();
for(int j=0;j<4;j++)
{
mat[i][j]=0;
}
for(int j=0;j<5;j++)
{
mat[j][k]=0;
}
}

//Printing the Resultant Zero Matrix 

for(int i=0;i<5;i++)
{
for(int j=0;j<4;j++)
{
 System.out.print(mat[i][j]);
}
System.out.println();
}
}
}
import java.util.*;
类零矩阵
{
公共静态void main(字符串参数[])
{
int mat[]][]=新int[]{{0,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,0};
HashMap ht=新的HashMap();

对于(inti=0;iYou有O(N^2)数据要处理,因此显然它不能比O(N^2)快。坚持你所拥有的。你必须访问每个单元格检查是否存在0,这样你就不能比O(m*N)做得更好了。你的方法对我来说似乎很直观,如何将每一行和每一列制作一个HashMap(你最终得到的是m+N HashMap))然后对每一个问题:
if(hmap.contains(0)){/*zero out the column or row of the column or row of it*/}
@RichardPlunkett M x N matrix(大写N)。M*N=N个要处理的数据。OP说
O(2n^2)
,你说
O(N^2)
而不是
数组列表
您应该使用
哈希集
,因为
包含
方法的成本将是O(1)而不是O(n)。对于行的归零,可以使用固定数组来降低内存成本。这与我的实现基本相同,但没有代码,您能解释一下设置位的含义吗?我很抱歉unfamiliar@EliteOctagon检查我的编辑。时间复杂度是一样的,但这更节省内存。我对你的逻辑很感兴趣,你能写一些吗编码和/或解释更多?不是今天。开始我只是想快速排序。它来自两个方面,将问题分解成更小的和平。然后我想如何以正确的方式将其分解。所以我想了一些代数函数。也许周日我可以给你一些解决方案。真正有趣的问题。我想我会的星期天晚上再试试。也许我能给你一些启发,即使我现在还没有解决方案。我想我们会在这里再次见面。我想时间复杂性是O(N^2)。因为你在这里使用的分区取O(N),对于N行,它是O(N^2).因此,问题最棘手的部分是如何记住行和列,我认为最好的方法是位集。确实如此。但我认为我的复杂性计算不正确。这个解决方案抛出了java.la
 0 1 2 4 6
public void zeroOut(int[][] myArray){
    ArrayList<Integer> rowsToZero = new....
    ArrayList<Integer> columnsToZero = new....

    for(int i=0; i<myArray.length; i++){ // record which rows and columns will be zeroed
        for(int j=0; j<myArray[i].length; i++){
            if(myArray[i][j] == 0){
                if(!rowsToZero.contains(i)) rowsToZero.add(i);
                if(!columnsToZero.contains(j)) columnsToZero.add(j);
            }
        }
    }
    for(int row : rows){ // now zero the rows
        myArray[row] = int[myArray.length];
    }

    for(int i=0; i<myArray.length; i++){
        for(int column: columns){ // now zero the columns
            myArray[i][column] = 0;
        }    
    }
}
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++;
        }
    }
}
import java.util.*;
class ZeroMatrix
{
public static void main(String args[])
{
int mat[][]=new int[][]{{0,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,0}};
HashMap<Integer,Integer> ht=new HashMap<Integer,Integer>();
for(int i=0;i<5;i++)
{
for(int j=0;j<4;j++)
{
if(mat[i][j]==0)
ht.put(i,j);
}
}

//Set the Respected Rows and colums to Zeros 
Set set=ht.entrySet();
Iterator itr=set.iterator();
while(itr.hasNext())
{
Map.Entry m=(Map.Entry)itr.next();
int i=(Integer)m.getKey();
int k=(Integer)m.getValue();
for(int j=0;j<4;j++)
{
mat[i][j]=0;
}
for(int j=0;j<5;j++)
{
mat[j][k]=0;
}
}

//Printing the Resultant Zero Matrix 

for(int i=0;i<5;i++)
{
for(int j=0;j<4;j++)
{
 System.out.print(mat[i][j]);
}
System.out.println();
}
}
}