矩阵置零

题意是要求在01矩阵中,把0的点的行和列都置零。
#include <iostream>
#include <vector>
#include <utility>
 
using namespace std;
 
void unguarded_setZero(int *matrix, int m, int n, int i, int j)
{
    for (int ii = 0; ii < m; ++ii)
    {
        *(matrix + ii * n + j) = 0;
    }
 
    for (int jj = 0; jj < n; ++jj)
    {
        *(matrix + i * n + jj) = 0;
    }
}
 
void solve(int *matrix, int m, int n)
{
    if (matrix == NULL || m <= 0 || n <= 0)
    {
        return;
    }
 
    vector<pair<int, int> > vecZero;
 
    for (int i = 0; i < m; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            if (*(matrix + i * n  + j) == 0)
            {
                vecZero.push_back(make_pair(i, j));
            }
        }
    }
 
    for (vector<pair<int, int> >::const_iterator it = vecZero.begin();
        it != vecZero.end();
        ++it)
    {
        const int i = it->first;
        const int j = it->second;
 
        unguarded_setZero(matrix, m, n, i, j);
    }
}
 
int main(int argc, char **argv)
{
    int matrix[][6] =
    {
        {1, 1, 0, 1, 1, 1},
        {0, 1, 1, 1, 0, 1},
        {1, 1, 1, 1, 1, 1}
    };
 
    solve((int *)matrix, 3, 6);
 
    for (size_t i = 0; i < sizeof(matrix) / sizeof(*matrix); ++i)
    {
        for (size_t j = 0; j < sizeof(*matrix) / sizeof(**matrix); ++j)
        {
            cout << matrix[i][j] << " ";
        }
 
        cout << endl;
    }
}
这次要求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;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值