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;
            
        }
    }
  • 相关阅读:
    cpuset
    top
    path-lookup
    strace
    IDR算法[原理]
    cgroup
    转载
    std::reverse_iterator::base
    可重入、不可重入
    chromium code 中 普遍使用的 C++11 语法
  • 原文地址:https://www.cnblogs.com/jasonC/p/3429955.html
Copyright © 2011-2022 走看看