lintcode-矩阵归零

来源:互联网 发布:linux有什么认证 编辑:程序博客网 时间:2024/04/30 20:06
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;            }        }    }}
原创粉丝点击