01矩阵相关
1.将01矩阵中,为0位置所在的行和列置为0
时间复杂度:O(m*n)
一行一行扫描,记录记录其中为0的列号
public void setZeroes(int[][] matrix) { boolean[] flag = new boolean[matrix[0].length]; for(int i =0; i<matrix.length; i++) { boolean contains0 = false; for (int j =0; j < matrix[0].length; j++) { if(matrix[i][j] == 0) { contains0 = true; flag[j] = true; } } if(contains0) for(int j=0; j<matrix[0].length; j++) matrix[i][j] = 0; } for(int j = 0; j < matrix[0].length; j++) if(flag[j]) for(int i =0; i<matrix.length; i++) matrix[i][j] = 0;
}
本文来自博客园,作者:LeeJuly,转载请注明原文链接:https://www.cnblogs.com/peterleee/p/11516137.html

浙公网安备 33010602011771号