1.7---将矩阵元素为0的行列清零0(CC150)

答案:

import java.util.ArrayList;
import java.util.List;

public class Solution{

    public static void main(String[] args){
        int[][] matix = {{1,2},{0,3}};
        clearZero(matix,2);
        System.out.println(matix[0][1]);
    }

    public static int[][] clearZero(int[][] matrix, int n){
        int high = matrix.length;
        int wide = matrix.length;
        if(high == 0) return matrix;
        List<Integer> listRow = new ArrayList();
        List<Integer> listCol = new ArrayList();
        for(int i = 0; i < wide; i++){
            for(int j = 0; j < high; j++){
                if(matrix[i][j] == 0){
                    listRow.add(i);
                    listCol.add(j);
                }
            }
        }
        for(int tmp : listRow){
            for(int j = 0; j < high; j++){
                matrix[tmp][j] = 0;
            }
        }
        for(int tmp : listCol){
            for(int i = 0; i < wide; i++){
                matrix[i][tmp] = 0;
            }
        }
        return matrix;
    }

 

posted @ 2015-12-16 16:50  创业-李春跃-增长黑客  阅读(399)  评论(0编辑  收藏  举报