package com.zmz.algorithm.array;
/**
* @author 张明泽
* Create by 2022/5/20 15:40
* 零矩阵
* LeetCode-面试题01.08.
*/
public class ZeroMatrix {
public static void main(String[] args) {
int[][] arr = {{0,1,2,0},{3,4,5,2},{1,3,1,5}};
zero(arr);
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
}
public static void zero(int[][] arr) {
int[] row = new int[arr.length];
int[] colomn = new int[arr[0].length]; // 避免3组数据 每组4个元素
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
if(arr[i][j] == 0) {
row[i] = 1;
colomn[j] = 1;
}
}
}
// System.out.println(Arrays.toString(row));
// System.out.println(Arrays.toString(colomn));
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
if(row[i] == 1 || colomn[j] == 1) {
arr[i][j] = 0;
}
}
}
}
}
最后修改:2022 年 05 月 25 日 02 : 05 PM
© 允许规范转载