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

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

    /**
     *O(1)空间解法,把每行的第一个元素和每列的第一个元素作为改行列是否清零的标志 
    **/
    public class Solution {
        public void setZeroes(int[][] matrix) {
            if (matrix==null) {
                return;
            }
            int row = matrix.length;
            int col = matrix[0].length;
            int[] zeroRow = new int[row];
            int[] zeroCol = new int[col];
            for (int i=0;i<row;i++) {
                for (int j=0;j<col;j++) {
                    if (matrix[i][j]==0) {
                        zeroRow[i] = 1;
                        zeroCol[j] = 1;
                    }
                }
            }
            for (int i=0;i<row;i++) {
                if (zeroRow[i]==1) {
                    for (int pcol=0;pcol<col;pcol++) {
                        matrix[i][pcol] = 0;
                    }
                }
            }
            for (int i=0;i<col;i++) {
                if (zeroCol[i]==1) {
                    for (int prow=0;prow<row;prow++) {
                        matrix[prow][i] = 0;
                    }
                }
            }
        }
    }
  • 相关阅读:
    GIT
    curl
    排序算法
    《软件设计师》考点分布
    lua第三方库
    WordPress
    go http
    Unity UI相关总结
    note
    LUA重难点解析
  • 原文地址:https://www.cnblogs.com/23lalala/p/3506870.html
Copyright © 2011-2022 走看看