TypechoJoeTheme

IT技术分享

统计

[LeetCode 73] Set Matrix Zeroes [Java] [Runtime: 1MS]

2017-08-14
/
0 评论
/
636 阅读
/
正在检测是否收录...
08/14

1. Description

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

2. Runtime Distribution

3. Submission Details

4. Example

given { { 1, 2, 3, 4 }, { 5, 0, 5, 2 }, { 8, 9, 2, 0 }, { 5, 7, 2, 1 } }
return
1 0 3 0
0 0 0 0
0 0 0 0
5 0 2 0

5. Code

package matrix;

public class LeetCode0073 {

    public void setZeroes(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return;
        }

        int lastRow = matrix.length - 1;
        int lastCol = matrix[0].length - 1;

        boolean isLastColNeedMark = false;

        for (int i = 0; i <= lastRow; i++) {
            if (matrix[i][lastCol] == 0) {
                isLastColNeedMark = true;
            }
            for (int j = 0; j < lastCol; j++) {
                if (matrix[i][j] == 0) {
                    matrix[i][lastCol] = 0;
                    matrix[0][j] = 0;
                }
            }
        }

        for (int j = 0; j < lastCol; j++) {
            if (matrix[0][j] == 0) {
                for (int i = 1; i <= lastRow; i++) {
                    matrix[i][j] = 0;
                }
            }
        }

        for (int i = lastRow; i >= 0; i--) {
            if (matrix[i][lastCol] == 0) {
                for (int j = 0; j < lastCol; j++) {
                    matrix[i][j] = 0;
                }
            }
            if (isLastColNeedMark) {
                matrix[i][lastCol] = 0;
            }
        }
    }

    public static void main(String[] args) {
        int[][] matrix = { { 1, 2, 3, 4 }, { 5, 0, 5, 2 },
                { 8, 9, 2, 0 }, { 5, 7, 2, 1 } };
        LeetCode0073 leetcode = new LeetCode0073();
        leetcode.setZeroes(matrix);
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}
Matrix
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/710/(转载时请注明本文出处及文章链接)