zoukankan      html  css  js  c++  java
  • 73. 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.

    链接: http://leetcode.com/problems/set-matrix-zeroes/

    题解:

    先判断矩阵第一行和第一列是否需要清零,然后再扫描矩阵把需要清零的行和列标注在第一行和第一列里, 最后进行清零操作。

    Time Complexity - O(m x n), Space Complexity - O(1)。

    public class Solution {
        public void setZeroes(int[][] matrix) {
            if(matrix == null || matrix[0].length == 0)
                return;
            int m = matrix.length, n = matrix[0].length;    
            Boolean firstRowZero = false, firstColZero = false;
            
            for(int i = 0; i < m; i ++){
                if(matrix[i][0] == 0){
                    firstColZero = true;
                    break;
                }
            }
            
            for(int j = 0; j < n; j ++){
                if(matrix[0][j] == 0){
                    firstRowZero = true;
                    break;
                }
            }
            
            for(int i = 1; i < m; i ++){
                for(int j = 1; j < n; j++){
                    if(matrix[i][j] == 0){
                        matrix[i][0] = 0;
                        matrix[0][j] = 0;
                    }
                }
            }
            
            for(int i = 1; i < m; i ++){
                for(int j = 1; j < n; j ++){
                    if(matrix[i][0] == 0 || matrix[0][j] == 0)
                        matrix[i][j] = 0;
                }
            }
            
            if(firstRowZero == true)
                for(int j = 0; j < n; j ++)
                    matrix[0][j] = 0;
            
            if(firstColZero == true)
                for(int i = 0; i < m; i ++)
                    matrix[i][0] = 0;
        }
    }

    Update:

    public class Solution {
        public void setZeroes(int[][] matrix) {
            if(matrix == null || matrix.length == 0)
                return;
            boolean isZeroInFirstRow = false, isZeroInFirstCol = false;
            
            for(int i = 0; i < matrix.length; i++)
                if(matrix[i][0] == 0) {
                    isZeroInFirstCol = true;
                    break;
                }
            
            for(int j = 0; j < matrix[0].length; j++)
                if(matrix[0][j] == 0) {
                    isZeroInFirstRow = 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[i][0] = 0;
                        matrix[0][j] = 0;
                    }
                        
                }
            }
                    
            for(int i = 1; i < matrix.length; i++) {
                for(int j = 1; j < matrix[0].length; j++) {
                    if(matrix[i][0] == 0 || matrix[0][j] == 0)
                        matrix[i][j] = 0;
                }
            }
            
            if(isZeroInFirstCol)
                for(int i = 0; i < matrix.length; i++)
                    matrix[i][0] = 0;
            
            if(isZeroInFirstRow) {
                for(int j = 0; j < matrix[0].length; j++)
                    matrix[0][j] = 0;
            }
        }
    }

    二刷:

    题目不难但是打字比较费力。

    Java:

    Time Complexity - O(mn), Space Complexity - O(1)

    public class Solution {
        public void setZeroes(int[][] matrix) {
            if (matrix == null || matrix.length == 0) {
                return;
            }
            boolean hasZeroFirstRow = false, hasZeroFirstCol = false;
            for (int i = 0; i < matrix.length; i++) {
                if (matrix[i][0] == 0) {
                    hasZeroFirstCol = true;
                    break;
                }
            }
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[0][j] == 0) {
                    hasZeroFirstRow = 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[i][0] = 0;
                        matrix[0][j] = 0;
                    }
                }
            }
            for (int i = 1; i < matrix.length; i++) {
                for (int j = 1; j < matrix[0].length; j++) {
                    if (matrix[i][0] == 0 || matrix[0][j] == 0) {
                        matrix[i][j] = 0;
                    }
                }
            }
            if (hasZeroFirstRow) {
                for (int j = 0; j < matrix[0].length; j++) {
                    matrix[0][j] = 0;
                }
            }
            if (hasZeroFirstCol) {
                for (int i = 0; i < matrix.length; i++) {
                    matrix[i][0] = 0;
                }
            }
        }
    }

    三刷:

    Java:

    public class Solution {
        public void setZeroes(int[][] matrix) {
            if (matrix == null || matrix.length == 0) return;
            boolean hasZeroFirstRow = false, hasZeroFirstCol = false;
            for (int i = 0; i < matrix.length; i++) {
                if (matrix[i][0] == 0) {
                    hasZeroFirstCol = true;
                    break;
                }
            }
            
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[0][j] == 0) {
                    hasZeroFirstRow = 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[i][0] = 0;
                        matrix[0][j] = 0;
                    }
                }
            }
            
            for (int i = 1; i < matrix.length; i++) {
                for (int j = 1; j < matrix[0].length; j++) {
                    if (matrix[i][0] == 0 || matrix[0][j] == 0) {
                        matrix[i][j] = 0;
                    }
                }
            }
            
            if (hasZeroFirstRow) {
                for (int j = 0; j < matrix[0].length; j++) matrix[0][j] = 0;
            }
            if (hasZeroFirstCol) {
                for (int i = 0; i < matrix.length; i++) matrix[i][0] = 0;
            }
        }
    }

    测试:

  • 相关阅读:
    敏捷开发 之 计划、测试 与 重构
    敏捷开发 之 理论概述篇
    第二十章 排查和调试Web程序 之 设计异常处理策略
    第十九章 排查和调试Web程序 之 防止和排查运行时问题
    第十八章 提升用户体验 之 减少带宽占用
    第十七章 提升用户体验 之 使用MVC扩展功能控制程序行为
    第十六章 提升用户体验 之 设计实现routes
    第十五章 提升用户体验 之 设计实现MVC controllers 和 actions
    第十四章 提升用户体验 之 设计实现国际化和本地化
    Nginx系统学习笔记(3)同一端口下设置Alias(虚拟目录)
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4437115.html
Copyright © 2011-2022 走看看