天天看點

LeetCode-Set Matrix Zeroes想法代碼結果傳回

作者:disappearedgod 文章出處:http://blog.csdn.net/disappearedgod/article/details/39269727 時間:2014-9-13

題目

Set Matrix Zeroes

  Total Accepted: 16929  Total Submissions: 54836 My Submissions

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

click to show follow up.

想法

代碼

public class Solution {
    public void setZeroes(int[][] matrix) {
        if(matrix.length == 0)
            return;
        if(matrix[0].length == 0)
            return;
        int max = matrix[0].length * matrix.length;
        Set line = new HashSet();
        Set row= new HashSet();

        for(int i = 0 ; i < matrix.length; i++){
            for(int j = 0; j < matrix[0].length; j++){
                if(matrix[i][j] == 0){
                    row.add(i);
                    line.add(j);
                }
            }
        }

        boolean rowSet = false;
        for(int i = 0; i<matrix.length; i++){
            if(row.contains(i))
                rowSet = true;
            else
                rowSet = false;
            for(int j = 0; j < matrix[0].length; j++){
                if(rowSet || line.contains(j))
                    matrix[i][j] = 0;
            }
        }
        
    }
}
           

結果

Submit Time Status Run Time Language
1 minute ago Accepted 484 ms java

傳回

 LeetCode Solution(持續更新,java>c++)