答案:
import java.util.ArrayList; import java.util.List; public class Solution{ public static void main(String[] args){ int[][] matix = {{1,2},{0,3}}; clearZero(matix,2); System.out.println(matix[0][1]); } public static int[][] clearZero(int[][] matrix, int n){ int high = matrix.length; int wide = matrix.length; if(high == 0) return matrix; List<Integer> listRow = new ArrayList(); List<Integer> listCol = new ArrayList(); for(int i = 0; i < wide; i++){ for(int j = 0; j < high; j++){ if(matrix[i][j] == 0){ listRow.add(i); listCol.add(j); } } } for(int tmp : listRow){ for(int j = 0; j < high; j++){ matrix[tmp][j] = 0; } } for(int tmp : listCol){ for(int i = 0; i < wide; i++){ matrix[i][tmp] = 0; } } return matrix; }
时间: 2024-11-10 11:59:42