LeetCode73. Set Matrix Zeroes(矩阵置零) JAVA实现

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

Example 1:

Input: 
[
  [1,1,1],
  [1,0,1],
  [1,1,1]
]
Output: 
[
  [1,0,1],
  [0,0,0],
  [1,0,1]
]

Example 2:

Input: 
[
  [0,1,2,0],
  [3,4,5,2],
  [1,3,1,5]
]
Output: 
[
  [0,0,0,0],
  [0,4,5,0],
  [0,3,1,0]
]

Follow up:

  • 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?

给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法

示例 1:

输入: 
[
  [1,1,1],
  [1,0,1],
  [1,1,1]
]
输出: 
[
  [1,0,1],
  [0,0,0],
  [1,0,1]
]

示例 2:

输入: 
[
  [0,1,2,0],
  [3,4,5,2],
  [1,3,1,5]
]
输出: 
[
  [0,0,0,0],
  [0,4,5,0],
  [0,3,1,0]
]

进阶:

  • 一个直接的解决方案是使用  O(mn) 的额外空间,但这并不是一个好的解决方案。
  • 一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。
  • 你能想出一个常数空间的解决方案吗?

解题思路:其实想要节省辅助空间,可以这样考虑,我们先遍历数组,记下每一个值为0的元素的行列下标,则该行该列的元素都应该全部置为0。

因为刚开始学习JAVA,想要实验一下JAVA的HashSet,于是建立一个集合set,将值为0的元素的行列下标存储到集合中,因为只要开一个集合,所以规定如果是行下标则直接存进去,如果是列下标,那么列下标+行数再存进去,这样拿出来的时候再判断一下即可。

代码实现一:

class Solution {
    public void setZeroes(int[][] matrix) {
        int m=matrix.length;
        int n=matrix[0].length;
        HashSet<Integer> set = new HashSet<Integer>();   //建立集合
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(matrix[i][j]==0){ 
                    set.add(i);   //行下标直接存储
                    set.add(j+m);  //列下标+m再存储
                }
            }
        }
        Iterator<Integer> s = set.iterator();//集合的迭代器
        int t;
        while(s.hasNext()){
            t = s.next();
            if(t<m){//判断是行下标还是列下标
                for(int i=0;i<n;i++)
                    matrix[t][i]=0;
            }
            else if(t>=m){
                t-=m;
                for(int i=0;i<m;i++)
                    matrix[i][t]=0;
            }
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                System.out.print(matrix[i][j]+" ");
            }
            System.out.println();
        }
    }
}

代码实现二:

唯一的区别是把上述代码中的集合改成了两个布尔值数组,用来记录0元素的行列下标。

class Solution {
    public void setZeroes(int[][] matrix) {
        if(matrix.length ==0 )
            return;
        int m=matrix.length;
        int n=matrix[0].length;
        boolean arr1[] = new boolean[m];  //一个行布尔值数组,判断某个行下标是0
        boolean arr2[] = new boolean[n];
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(matrix[i][j]==0){
                    arr1[i]=true;
                    arr2[j]=true;
                }
            }
        }
        for(int i=0;i<m;i++){
            if(arr1[i]==true){
                for(int j=0;j<n;j++)
                    matrix[i][j]=0;
            }
        }
        for(int j=0;j<n;j++){
            if(arr2[j]==true){
                for(int i=0;i<m;i++)
                    matrix[i][j]=0;
            }
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值