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

LeetCode -- Set Matrix Zeroes

时间:2016-03-31 23:32:16      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

Question:

 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

 

Analysis:

给出一个m * n的矩阵,如果一个元素为0,则它整行整列都设为0.

 

Answer:

public class Solution {
    public void setZeroes(int[][] matrix) {
        ArrayList<Integer> colll = new ArrayList<Integer>();
        if(matrix == null)
            return;
        
        int row = matrix.length, col = matrix[0].length;
        boolean flag = false;
        for(int i=0; i<row; i++) {
            flag = false;
            for(int j=0; j<col; j++) {
                if(matrix[i][j] == 0) {
                    flag = true;
                    if(!colll.contains(j))
                        colll.add(j);
                }
            }
            if(flag == true) {
                for(int j=0; j<col; j++)
                matrix[i][j] = 0;
            }
        }
        
        for(int j=0; j<colll.size(); j++) {
            int temp = colll.get(j);
            for(int i=0; i<row; i++)
                matrix[i][temp] = 0;
        }
    }
}

 

LeetCode -- Set Matrix Zeroes

标签:

原文地址:http://www.cnblogs.com/little-YTMM/p/5343090.html

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