这个题乍一看非常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. C# - 通过自定义注解反射生成SQL语句[转]

    转自http://blog.163.com/jong_cai/blog/static/87028045200902033553581/ -------------------------------- ...

  2. [Swust OJ 632]--集合运算(set容器)

    题目链接:http://acm.swust.edu.cn/problem/632/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  3. POJ 3376 Finding Palindromes(扩展kmp+trie)

    题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...

  4. C中的正则函数sscanf

    头文件 #include 定义函数 int sscanf (const char *str,const char * format,........); 函数说明 sscanf()会将参数str的字符 ...

  5. setTimeout 虚假的“异步”

    看这篇http://www.laruence.com/2009/09/23/1089.html 所以实际上 setTimeout更像是任务按照队列执行  经过setTimeout设置后任务放在了队尾 ...

  6. AMD模块化JS

    参考http://ourjs.com/detail/52ad26ff127c76320300001f Offcial Site http://requirejs.org/ 下载http://requi ...

  7. IMP-00008: unrecognized statement in the export file: string的问题分析

    分类: Linux 上周需要将oracle10g中的某一个用户下的对象导入到oracle11g中去.用exp在10g的数据库服务器上导出的dump文件,再用imp在11g的数据库服务器上将dump文件 ...

  8. Understanding and Selecting a SIEM/LM: Correlation and Alerting

    Continuing our discussion of core SIEM and Log Management technology, we now move into event correla ...

  9. Protel99se教程二:使用protel99se原理图绘制

    使用protel99se绘制原理图,首先要先设置一下显示网格这一项,这个可以根据个人习惯,并不是一定需要这样的,在prote99se的界面的View菜下,将visible Grid选中或取消,可以选择 ...

  10. SpringBoot Quickstart

    SpringBoot Intro SpringBoot是顺应现在微服务(MicroServices)理念而产生的一个微框架(同类微框架可供选择的还有Dropwizard), 用来构建基于Spring框 ...