[leetcode]73. Set Matrix Zeroes

Solution 1: O(MN)
class Solution {
    public void setZeroes(int[][] matrix) {
        int[][] newMatrix=new int[matrix.length][matrix[0].length];
         for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[0].length;j++){
               newMatrix[i][j]=matrix[i][j];
            }
        }
        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[0].length;j++){
                if(matrix[i][j]==0){
                    setZ(matrix,newMatrix,i,j);
                }
            }
        }
         for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[0].length;j++){
                   
                matrix[i][j]=newMatrix[i][j];
                
            }
        }
       
    
        
    }
    
    public void setZ(int[][] matrix,int[][] newMatrix,int r,int c){
        for(int i=0;i<matrix[0].length;i++){
            newMatrix[r][i]=0;
        }
        for(int i=0;i<matrix.length;i++){
            newMatrix[i][c]=0;
        }
        
        
        
        
    }
}
Solution 2: O(1)

用另一个数来代替0
但是这个方法实际是不对的,因为总会有case正好是你设的那个marker

class Solution {
    public void setZeroes(int[][] matrix) {
        
        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[0].length;j++){
                if(matrix[i][j]==0){
                    for(int m=0;m<matrix[0].length;m++){
                        if(matrix[i][m]!=0)
                        matrix[i][m]=Integer.MIN_VALUE;
                    }
                    for(int k=0;k<matrix.length;k++){
                        if(matrix[k][j]!=0)
                        matrix[k][j]=Integer.MIN_VALUE;
                    }
        
                }
            }
        }
         for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[0].length;j++){
                 if(matrix[i][j]==Integer.MIN_VALUE)  
                     matrix[i][j]=0;
                
            }
        }
       
    
        
    }
    
   
}
Solution 3: O(1), 只用了两个space
class Solution {
  public void setZeroes(int[][] matrix) {
   
    int R = matrix.length;
    int C = matrix[0].length;
    Boolean fr=false,fc=false;
    //先标记第0行和第0列是否需要update
      for(int i=0;i<C;i++){
          if(matrix[0][i]==0)fr=true;
      }
      for(int j=0;j<R;j++){
          if(matrix[j][0]==0)fc=true;
      }

//扫描,更新标记
    for (int i=1;i<R;i++){
        for(int j=1;j<C;j++){
            if(matrix[i][j]==0){
                matrix[i][0]=0;
                matrix[0][j]=0;
            }
            
        }
    }
      //根据标记来update
    for(int i=1;i<R;i++){
        for(int j=1;j<C;j++){
            if(matrix[i][0]==0||matrix[0][j]==0){
                matrix[i][j]=0;
            }
        }
    }
      
      //最后处理作为标记的第0行和第0列
    if(fr){
        for(int i=0;i<C;i++)matrix[0][i]=0;
    }
      
    if(fc){
        for(int i=0;i<R;i++)matrix[i][0]=0;
    }
  }
}
Solution 4:O(1),只用了两个space,感觉没有3好想

第0行和第0列是做标记,从第一列开始扫描。
只要标记为0,那么就update所有除第0行和第0列的数字。
最后因为第0行和第0列被用作标记了,所以要特殊处理。

只要有行的第一个是0,那么isCol就是true,整个第一列要update。
只要第0行的列里面有0,就一定会把matrix[0][0]设0

所以只要两个空间。

class Solution {
  public void setZeroes(int[][] matrix) {
    Boolean isCol = false;
    int R = matrix.length;
    int C = matrix[0].length;

    for (int i = 0; i < R; i++) {

      // Since first cell for both first row and first column is the same i.e. matrix[0][0]
      // We can use an additional variable for either the first row/column.
      // For this solution we are using an additional variable for the first column
      // and using matrix[0][0] for the first row.
      if (matrix[i][0] == 0) {
        isCol = true;
      }

      for (int j = 1; j < C; j++) {
        // If an element is zero, we set the first element of the corresponding row and column to 0
        if (matrix[i][j] == 0) {
          matrix[0][j] = 0;
          matrix[i][0] = 0;
        }
      }
    }

    // Iterate over the array once again and using the first row and first column, update the elements.
    for (int i = 1; i < R; i++) {
      for (int j = 1; j < C; j++) {
        if (matrix[i][0] == 0 || matrix[0][j] == 0) {
          matrix[i][j] = 0;
        }
      }
    }

    // See if the first row needs to be set to zero as well
    if (matrix[0][0] == 0) {
      for (int j = 0; j < C; j++) {
        matrix[0][j] = 0;
      }
    }

    // See if the first column needs to be set to zero as well
    if (isCol) {
      for (int i = 0; i < R; i++) {
        matrix[i][0] = 0;
      }
    }
  }
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值