package bd_algo;
import java.util.ArrayList;
/**
* Created by Administrator on 2017/8/29.
*/
class MyPoint {
int x;
int y;
MyPoint(int x, int y) {
this.x = x;
this.y = y;
}
}
public class setZero {
public static void main(String[] args) {
}
public void setZeroes(int[][] matrix) {
// write your code here
if (matrix == null || matrix.length == 0) return;
int rows = matrix.length;
int cols = matrix[0].length;
ArrayList<MyPoint> points = new ArrayList<>();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 0) {
points.add(new MyPoint(i, j));
}
}
}
for (MyPoint point :
points) {
int x = point.x;
int y = point.y;
for (int i = 0; i < cols; i++) {
matrix[x][i] = 0;
}
for (int i = 0; i < rows; i++) {
matrix[i][y] = 0;
}
}
}
}
lintcode-矩阵归零
最新推荐文章于 2022-12-16 19:14:20 发布