这个题乍一看非常easy,实际上还挺有技巧的。我最開始的想法是找一个特殊值标记。遇到一个0,把他所相应的行列中非零的元素标记成这个特殊值。0值保持不变,然后再从头遍历一次,碰到特殊值就转化成0。

问题是这个特殊值怎么确定,题目中没有把取值范围给出,我怀着侥幸的心理用了最大和最小的int,都被揪了出来。。假设找一个不存在于数组中的值,这个复杂度太高了。

有没有其它更好的方法呢?当然有。这个思想非常巧妙,最后的结果是把所有0所在的行列都化成0,换句话说。化成0这个事情仅仅要标记出是哪一行以及哪一列就能够了。能不能找到一种标记的方式来完毕这个事情呢?做法是假设扫描到一个0。我们就把这个数的行列第一个数值置成0,用来做标记位,等所有扫描完毕后,针对为第一行和第一列为0的那些行列,置成0就能够了。

有一个细节,第一行和第一列该怎么办,他们可能一開始并没有0,由于标记的原因有0了。怎么区分呢?处理方式事实上非常easy,一開始先看一下它含不含0。然后在最后依据这个结果来决定要不要把他们变成0.

class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
int row = matrix.size(), column = matrix[0].size();
bool firstRow = false, firstColumn = false;
for(int i=0;i<row;i++)
if(matrix[i][0] == 0){
firstColumn = true;
break;
}
for(int i=0;i<column;i++)
if(matrix[0][i] == 0){
firstRow = true;
break;
}
for(int i=1;i<row;i++){
for(int j=1;j<column;j++){
if(matrix[i][j] == 0){
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for(int i=1;i<row;i++){
for(int j=1;j<column;j++){
if(matrix[i][0] == 0 || matrix[0][j] == 0)
matrix[i][j] = 0;
}
}
if(firstRow){
for(int i=0;i<column;i++)
matrix[0][i] = 0;
}
if(firstColumn){
for(int i=0;i<row;i++)
matrix[i][0] = 0;
}
}
};

leetcode第一刷_Set Matrix Zeroes的更多相关文章

  1. leetcode第一刷_Spiral Matrix

    我认为这个题好无聊啊,好端端一个数组.干嘛要跟比巴卜一样转一圈输出呢. . 思想非常easy,每次从左到右.再从上到下,在从右到左,再从下到上.问题是每次到什么时候该改变方向.我的做法是用一个变量保存 ...

  2. leetcode第一刷_Spiral Matrix II

    跟上一题的策略全然一样,这个题是要求保存当前增加的是第几个数,由于矩阵里面存的就是这个东西. 我有尝试想过是不是有一种方法能够直接推算出每一行的数据是哪些.但没过多久就放弃了.这样的方法尽管能够避免在 ...

  3. 【LeetCode】73. Set Matrix Zeroes (2 solutions)

    Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do i ...

  4. Leetcode 细节实现 Set Matrix Zeroes

    Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...

  5. 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...

  6. LeetCode OJ 73. Set Matrix Zeroes

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

  7. 【LeetCode】73. Set Matrix Zeroes

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

  8. 【一天一道LeetCode】#73. Set Matrix Zeroes

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. 【LeetCode】-- 73. Set Matrix Zeroes

    问题描述:将二维数组中值为0的元素,所在行或者列全set为0:https://leetcode.com/problems/set-matrix-zeroes/ 问题分析:题中要求用 constant ...

随机推荐

  1. SSIS DB目录设置 (Integration Services Catalogs)

    1.创建SSISDB目录 这里没什么好说的,点击Enable CLR Integration ,然后设置一个加密密码 2. SSIS Catalog设置 Retention Period (days) ...

  2. ExtJS001HelloWorld弹窗

    html页面 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...

  3. VS2012 内容存储区指定的位置无效或者您无权访错误

    ——解决由于移动过microsoft help viwer( msdn )数据目录,又误删除数据目录后,引发其不能启动问题 1.使用命令行下载microsoft help viwer( msdn )数 ...

  4. C++基础知识梳理--C++的6个默认函数

    C++有六个默认函数:分别是 1.default构造函数; 2.默认拷贝构造函数; 3.默认析构函数; 4.赋值运算符; 5.取值运算符; 6.取值运算符const; // 这两个类的效果相同 cla ...

  5. Oracle分区知识

    查询分区名称.表空间的SQL USER_SEGMENTS SELECT SEGMENT_NAME,PARTITION_NAME,TABLESPACE_NAME FROM USER_SEGMENTS; ...

  6. 【转】使用Boost Graph library(一)

    转自:http://shanzhizi.blog.51cto.com/5066308/942970 本文是一篇译文,来自:http://blog.csdn.net/jjqtony/article/de ...

  7. MVC模式已死

    MVC模式:Model模型 View试图 Control控制器,是目前主流模式,被当作服务器软件入门基本模式学习和掌握,主流框架Struts 1/2 JSF Wicket基本都顺理成章支持MVC模式. ...

  8. cocos2d基础篇笔记四

    1.//有两种集合 //第一种是array 特点:插入,删除效率低,可是查找效率高 //另外一种是list  特点:插入,删除效率高,可是查找效率低 //分析这个游戏: 插入的时候:怪物,射弹出现时, ...

  9. Qt5官方demo分析集11——Qt Quick Particles Examples - Affectors

    在这个系列中的所有文章都可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集10--Qt ...

  10. Android 建造者(Builder)模式

    关于 Builder 模式 详述:http://blog.csdn.net/jjwwmlp456/article/details/39890699 先来张图 看到 Android  中 使用了 Bui ...