码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-12-16 18:49:20      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

答案:

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;
    }

 

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

标签:

原文地址:http://www.cnblogs.com/yueyebigdata/p/5051610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!