p73

LeetCode p73 Set Matrix Zeroes 题解

1.题目:

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

题意:

给一个矩阵,把有0的行列全部设置为0.

2.解题思路:

见代码

3.代码


[title] [] [url] [link text]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
public class Solution {
public void setZeroes(int[][] matrix) {
int n = matrix.length;
if (n < 1)
return;
int m = matrix[0].length;
if (m < 1)
return;
HashSet<Integer> n1 = new HashSet<Integer>();
HashSet<Integer> m1 = new HashSet<Integer>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == 0) {
n1.add(i);
m1.add(j);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {

if (n1.contains(i) || m1.contains(j)) {
matrix[i][j] = 0;
}
}
}
}
}

4.一些总结: