作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/set-matrix-zeroes/solution/ju-zhen-zhi-ling-by-leetcode-solution-9ll7/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明
个人微改:
import java.util.Arrays;
class zeroes{
public void setZeroes(int[][] matrix) {
int m = matrix.length,n = matrix[0].length;
boolean[] row = new boolean[m];
boolean[] col = new boolean[n];
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
if(matrix[i][j] == 0) {
row[i] = col[j] = true;
}
}
}
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
if(row[i] || col[j]) {
matrix[i][j] = 0;
}
}
}
}
}
public class SetMatrixZeroes {
public static void main(String args[]) {
zeroes hah = new zeroes();
int[][] matrix = new int[][] {{1,1,1},{1,0,1},{1,1,1}};
hah.setZeroes(matrix);
System.out.println(Arrays.deepToString(matrix));
}
}

该博客介绍了一个Java实现的矩阵置零问题解决方案,通过双指针遍历矩阵,记录行和列是否有零,然后二次遍历将对应位置设为零,提高了效率。
2140

被折叠的 条评论
为什么被折叠?



