LeetCode Set Matrix Zeroes

class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix) {
        int rows = matrix.size();
        int cols = matrix[0].size();
        
        bool col_has_zero = false;
        bool row_has_zero = false;
        
        for (int i=0; i<cols; i++) {
            if (matrix[0][i] == 0) { 
                row_has_zero = true;
                break;
            }
        }
        
        for (int i=0; i<rows; i++) {
            if (matrix[i][0] == 0) {
                col_has_zero = true;
                break;
            }
        }
        
        for (int i=1; i<rows; i++) {
            for (int j=1; j<cols; j++) {
                if (matrix[i][j] != 0) continue;
                matrix[i][0] = 0;
                matrix[0][j] = 0;
            }
        }
        
        for (int i=1; i<rows; i++) {
            for (int j=1; j<cols; j++) {
                if (!matrix[i][0] || !matrix[0][j]) {
                    matrix[i][j] = 0;
                }
            }
        }

        for (int i=0; row_has_zero && i<cols; i++) matrix[0][i] = 0;
        for (int i=0; col_has_zero && i<rows; i++) matrix[i][0] = 0;

    }
};

没意思,搞个几个bit的额外空间会死么

第二轮:

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.

Follow up:

Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?

class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix) {
        bool first_row_zero = false;
        bool first_col_zero = false;
        
        int rows = matrix.size();
        if (rows < 1) {
            return;
        }
        int cols = matrix[0].size();
        if (cols < 1) {
            return;
        }
        
        for (int i=0; i<cols; i++) {
            if (matrix[0][i] == 0) {
                first_row_zero = true;
                break;
            }
        }
        
        for (int i=0; i<rows; i++) {
            if (matrix[i][0] == 0) {
                first_col_zero = true;
                break;
            }
        }
        
        for (int i=1; i<rows; i++) {
            for (int j=1; j<cols; j++) {
                if (matrix[i][j] == 0) {
                    matrix[0][j] = 0;
                    matrix[i][0] = 0;
                }
            }
        }
        
        for (int i=1; i<rows; i++) {
            if (matrix[i][0] == 0) {
                for (int j=1; j<cols; j++) matrix[i][j] = 0;
            }
        }
        
        
        for (int i=1; i<cols; i++) {
            if (matrix[0][i] == 0) {
                for (int j=1; j<rows; j++) matrix[j][i] = 0;
            }
        }
        
        for (int i=0; i<cols && first_row_zero; i++) matrix[0][i] = 0;
        for (int i=0; i<rows && first_col_zero; i++) matrix[i][0] = 0;
    }
};

 

没有一次AC,最后做填充的时候应该出去最左边的列和最上面的行

posted @ 2014-07-18 15:52  卖程序的小歪  阅读(139)  评论(0编辑  收藏  举报