【Set Matrix Zeros】cpp

题目

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) {
            const size_t size_row = matrix.size();
            const size_t size_col = matrix[0].size();
            bool if_row0_has_zero = false;
            bool if_col0_has_zero = false;
            for (size_t col = 0; col < size_col; ++col){
                if (matrix[0][col]==0){
                    if_row0_has_zero = true;
                    break;
                }
            }
            for (size_t row = 0; row < size_row; ++row){
                if (matrix[row][0]==0){
                    if_col0_has_zero = true;
                    break;
                }
            }
            for (size_t row = 0; row < size_row; ++row){
                for (size_t col = 0; col < size_col; ++col){
                    if (matrix[row][col]==0){
                        matrix[row][0] = 0;
                        matrix[0][col] = 0;
                    }
                }
            }
            for (size_t row = 1; row < size_row; ++row){
                for (size_t col = 1; col < size_col; ++col){
                    if ( matrix[row][0]==0 || matrix[0][col]==0 ) matrix[row][col]=0;
                }
            }
            if (if_row0_has_zero) {
                for (size_t col = 0; col < size_col; ++col) matrix[0][col]=0;
            }
            if (if_col0_has_zero){
                for (size_t row = 0; row < size_row; ++row) matrix[row][0]=0;
            }
    }
};
复制代码

 

Tips:

1. 算法时间复杂度上是O(n²)

2. 这里为了节省空间复杂度,用到的技巧是把第一行和第一列作为该行或该列是否含有0元素的标志位,这样就可以在常数空间内完成题目。

========================================================

第二次过这道题,思路比较清晰,代码也一次AC了。

复制代码
class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
            if (matrix.size()==0) return;
            // check first row
            bool first_row_zero = false;
            for ( int j=0; j<matrix[0].size(); ++j ) { 
                if (matrix[0][j]==0) {first_row_zero=true;break;}
            }
            // check frist col
            bool first_col_zero = false;
            for ( int j=0; j<matrix.size(); ++j ) { 
                if ( matrix[j][0]==0) {first_col_zero=true;break;}
            }
            // check remains
            for ( int i=1; i<matrix.size(); ++i )
            {
                for ( int j=1; j<matrix[i].size(); ++j )
                {
                    if ( matrix[i][j]==0 )
                    {
                        matrix[0][j] = 0;
                        matrix[i][0] = 0;
                    }
                }
            }
            // set row zeros
            for ( int i=1; i<matrix.size(); ++i )
            {
                if ( matrix[i][0]==0 )
                {
                    for ( int j=1; j<matrix[i].size(); ++j ) matrix[i][j] = 0;
                }
            }
            // set col zeros
            for ( int j=1; j<matrix[0].size(); ++j )
            {
                if ( matrix[0][j]==0 )
                {
                    for ( int i=1; i<matrix.size(); ++i ) matrix[i][j] = 0;
                }
            }
            if (first_row_zero)
            {
                for ( int j=0; j<matrix[0].size(); ++j ) matrix[0][j] = 0;
            }
            if ( first_col_zero)
            {
                for ( int i=0; i<matrix.size(); ++i ) matrix[i][0] = 0;
            }
    }
};
复制代码

这个代码大体上没有问题,但是在一个部分是可以优化的。就是set row zeros和set col zeros两个部分可以合成一个,改一版代码如下。

复制代码
class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
            if (matrix.size()==0) return;
            // check first row
            bool first_row_zero = false;
            for ( int j=0; j<matrix[0].size(); ++j ) { 
                if (matrix[0][j]==0) {first_row_zero=true;break;}
            }
            // check frist col
            bool first_col_zero = false;
            for ( int j=0; j<matrix.size(); ++j ) { 
                if ( matrix[j][0]==0) {first_col_zero=true;break;}
            }
            // check remains
            for ( int i=1; i<matrix.size(); ++i )
            {
                for ( int j=1; j<matrix[i].size(); ++j )
                {
                    if ( matrix[i][j]==0 )
                    {
                        matrix[0][j] = 0;
                        matrix[i][0] = 0;
                    }
                }
            }
            // set zeros
            for ( int i=1; i<matrix.size(); ++i )
            {
                for ( int j=1; j<matrix[i].size(); ++j )
                {
                    if ( matrix[i][0]==0 || matrix[0][j]==0 ) matrix[i][j]=0;
                }
            }
            if (first_row_zero)
            {
                for ( int j=0; j<matrix[0].size(); ++j ) matrix[0][j] = 0;
            }
            if ( first_col_zero)
            {
                for ( int i=0; i<matrix.size(); ++i ) matrix[i][0] = 0;
            }
    }
};
复制代码

这样改版后,代码效率提升了。

posted on   承续缘  阅读(181)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示