题解 | #矩阵置零#

矩阵置零

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));
                    }
                }
            }
        }
    }
}
全部评论

相关推荐

求个公司要我:接好运
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务