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

    public class Solution {
        public void setZeroes(int[][] matrix) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            if(matrix == null || matrix.length == 0)
                return;
            boolean column = false;
            boolean row = false;
            
            for(int i = 0; i < matrix.length; i++)
                if(matrix[i][0] == 0){
                    column = true;
                    break;
                }
            for(int j = 0; j < matrix[0].length; j++)
                if(matrix[0][j] == 0){
                    row = true;
                    break;
                }
                
            for(int i = 1; i < matrix.length; i++)
                for(int j = 1; j < matrix[0].length; j++){
                    if(matrix[i][j] == 0){
                        matrix[0][j] = 0;
                        matrix[i][0] = 0;
                    }
                }
            
            for(int i = 1; i < matrix.length; i++){
                if(matrix[i][0] == 0)
                    for(int j = 1; j < matrix[0].length; j++)
                        matrix[i][j] = 0;
            }
            
            for(int j = 1; j < matrix[0].length; j++){
                if(matrix[0][j] == 0)
                    for(int i = 1; i < matrix.length; i++)
                        matrix[i][j] = 0;
            }
            
            if(column)
                for(int i = 0; i < matrix.length; i++)
                    matrix[i][0] = 0;
            if(row)
                for(int j = 0; j < matrix[0].length; j++)
                    matrix[0][j] = 0;
            
        }
    }
  • 相关阅读:
    商场活动|简单易用|可下载试用|复用转盘抽奖软件
    js dictionary
    财务大写
    SET ANSI_NULLS ON ……
    批量生成clr脚本
    Git
    CTE递归查询
    jquery 巧用json传参
    个人犯的一个golang routine错误
    .NET实现自动编译
  • 原文地址:https://www.cnblogs.com/jasonC/p/3429955.html
Copyright © 2011-2022 走看看