题解 | #矩阵置零#
矩阵置零
http://www.nowcoder.com/practice/e77f5c093978493396b22b9bc01af12d
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param matrix int整型ArrayList<ArrayList<>>
*/
public void setZeroMatrix (ArrayList<ArrayList<Integer>> matrix) {
// write code here
int n = matrix.size();
int m = matrix.get(0).size();
int[][] sign = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix.get(i).get(j) == 0) {
sign[i][j] = 1;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix.get(i).get(j) == 0 && sign[i][j] == 1) {
for (int k = 0; k < j; k++) {
matrix.get(i).set(k, Integer.valueOf(0));
}
for (int k = j + 1; k < m; k++) {
matrix.get(i).set(k, Integer.valueOf(0));
}
for (int k = 0; k < i; k++) {
matrix.get(k).set(j, Integer.valueOf(0));
}
for (int k = i + 1; k < n; k++) {
matrix.get(k).set(j, Integer.valueOf(0));
}
}
}
}
}
}