LeetCode---set-matrix-zeros

hi,我终于开通了自己的csdn博客,想在这里记录自己成长的点点滴滴。从算法开始,leetcode刷题。

LeetCode ----set-matrix-zeros(空间复杂度O(1));


原题描述:

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

Follow up:(Important,things to be thought over)

Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?

函数入口为:public void setZeroes(int[][] matrix);

首先解释下题目的意思:一个m×n的矩阵,如果某个元素为零,则将该行和该列都置0。


解题思路及逐步改进:

1,思考是否可以直接遍历:在遍历过程中若发现为0的元素,则可以再开一个遍历,讲该行和该列置0。(错误×)原因自己思考;微笑

2,最简单暴力的方法(空间复杂度为O(mn)):另开辟一个m×n的矩阵,记录原矩阵中每个元素是否为零,记录完后再根据该矩阵进行置0操作;缺点:每行或每列只要有一个0,该行和该列最后都会变为0,因此没必要记录每个元素,可以按行或者按列记录是否有0元素;

3,较简单的改进(空间复杂度为O(m+n)):可开辟两个一维数组row[m]和col[n],记录对应行和列是否存在0元素。

----------------------最最最最精彩和重要的彩蛋来了------------------------------

4,由于题目提示让我们去思考能否用常量辅助空间来解决该问题,总结了一下其他大神的思路:

常数空间的话,第一可以考虑是不是固定数量的几个变量能搞定;否则可以考虑是不是问题本身已经提供了足够的空间。

该题我们从第二条入手思考:利用二维数组本身的空间作为记录数组。例如,使用矩阵的第一行和第一列来代替第3步中的辅助空间,去记录除第一行和第一列外剩下的部分是否有0元素,那么第一行和第一列中的0元素如何记录呢?开辟一个bool值来记录(常量空间)。哈哈哈哈哈。下面附上代码实现。(另:leetcode讨论区的解题算法没有最好,只有更好,这个思路是借鉴了讨论区中该题排名第一的解法;)

public class Set_Matrix_Zeros {
    public static void setZeros(int[][] matrix) {
        //首先进行参数判断(此处注意二维数组行数和列数的表示)
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return;
        int i = 0, j = 0, rows = matrix.length, cols = matrix[0].length;
        boolean row0_has_zero = false;


        //判断第一行是否有0
        for (j = 0; j < cols; j++)
            if (matrix[0][j] == 0) {
                row0_has_zero = true;
                break;
            }

        //判断第一列是否有0
        for (i = 0; i < rows; i++)
            if (matrix[i][0] == 0) {
                matrix[0][0] = 0;
                break;
            }

        //寻找0操作
        for (i = 1; i < rows; i++)
            for (j = 1; j < cols; j++)
                if (matrix[i][j] == 0) {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }


        //0操作(先将除第一行和第一列的置0        for (j = 1; j < cols; j++)
            if (matrix[0][j] == 0) {
                for (i = 1; i < rows; i++)
                    matrix[i][j] = 0;
            }
        for (i = 1; i < rows; i++)
            if (matrix[i][0] == 0) {
                for (j = 1; j < cols; j++)
                    matrix[i][j] = 0;
            }

        if (matrix[0][0] == 0) {
            for (i = 1; i < rows; i++)
                matrix[i][0] = 0;
        }

        if (row0_has_zero) {
            for (j = 0; j < cols; j++)
                matrix[0][j] = 0;
        }

    }

    public static void printMatrix(int[][] matrix) {
        if(matrix==null||matrix.length==0||matrix[0].length==0)return;
        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(" ");
        }
        System.out.println("+++++++++++");
    }
    public static void main(String[] args) {
        int[][] matrix={{1,2,3,4,5},{9,0,7,8,4},{1,1,1,0,1}};
        printMatrix(matrix);
        setZeros(matrix);
        printMatrix(matrix);
    }
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值