Set Matrix Zeroes

      还是把矩阵中值为0的点的行列都置零,上次是开辟了额外空间,这次要求O(1)的空间复杂度。

void setZeroes(vector<vector<int> > &matrix) 
{
    bool bColZero = false, bRowZero = false;
    
    if (matrix.size() == 0 || matrix[0].size() == 0)
    {
        return;
    }
 
    // Mark bColZero true when col[0] contains zero.   
    for (size_t row = 0; row < matrix.size(); ++row)
    {
        if (!matrix[row][0]) bColZero = true;
    }
    
    // Mark bRowZero true when row[0] contains zero.
    for (size_t col = 0; col < matrix[0].size(); ++col)
    {
        if (!matrix[0][col]) bRowZero = true;
    }
    
    // Map zero points to row[0] and col[0].
    for (size_t row = 1; row < matrix.size(); ++row)
    {
        for (size_t col = 1; col < matrix[row].size(); ++col)
        {
            if (!matrix[row][col])
            {
                matrix[0][col] = 0;
                matrix[row][0] = 0;
            }
        }
    }
    
    // Set zero according to row[0] and col[0].
    for (size_t row = 1; row < matrix.size(); ++row)
    {
        for (size_t col = 1; col < matrix[row].size(); ++col)
        {
           if (!matrix[row][0] || !matrix[0][col])
           {
               matrix[row][col] = 0;
           }
        }
    }
    
    // Process col[0].
    if (bColZero)
    {
        for (size_t row = 0; row < matrix.size(); ++row)
        {
            matrix[row][0] = 0;
        }
    }
    
    // Process row[0].
    if (bRowZero)
    {
        for (size_t col = 0; col < matrix[0].size(); ++col)
        {
            matrix[0][col] = 0;
        }
    }
}
posted @ 2012-10-07 14:13  紫红的泪  阅读(1867)  评论(0编辑  收藏  举报