zoukankan      html  css  js  c++  java
  • Set Matrix Zeros

    public class Solution {
        public void setZeroes(int[][] matrix) {
           if(matrix.length == 0) {
               return;
           }
           
           int r = matrix.length;
           int c = matrix[0].length;
          
           boolean rZeros = false;
           boolean cZeros = false;
           
           for(int i = 0 ; i < r ; i++){
               if(matrix[i][0] == 0){
                   rZeros = true;
                   break;
               }
           }
           
           for(int j = 0 ; j < c ; j++){
               if(matrix[0][j] == 0){
                   cZeros = true;
                   break;
               }
           }
           
           
           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;
                   }
               }
           }
           
           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;
                   }
               }
           }
           
           if(rZeros) {
               for(int i = 0; i< r ; i++){
                   matrix[i][0] = 0;
               }
           }
           
           if(cZeros) {
               for(int j = 0; j< c;j++){
                   matrix[0][j] = 0;
               }
           }
           
           
            
        }
    }
  • 相关阅读:
    【leetcode】对称二叉树
    【leetcode】判断回文数
    053686
    053685
    053684
    053683
    053682
    053681
    053680
    053477
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3532287.html
Copyright © 2011-2022 走看看