Question:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Solution:
如何合理的用hashset,查找到有0的行和列~存下
public class Solution {
public void setZeroes(int[][] matrix) {
HashSet<Integer>row=new HashSet<Integer>();
HashSet<Integer>col=new HashSet<Integer>();
int m=matrix.length;
int n=matrix[0].length;
for(int i=0;i<=m-1;i++)
{
for(int j=0;j<=n-1;j++)
{
if(matrix[i][j]==0)
{
row.add(i);
col.add(j);
}
}
}
for(int i=0;i<=m-1;i++)
{
for(int j=0;j<=n-1;j++)
{
if(row.contains(i)||col.contains(j))
{
matrix[i][j]=0;
}
}
}
}
}