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

重点是空间复杂度限制为常数.

人家想法:

用 matrix 的第0行和第0列的元素分别记录所对应的行和列是否有0.

A[0][0]比较特殊,我们只让它记录第0行的情况,声明另一变量col0记录第0列的情况.

我的实现代码较长,但思路表达的相当清楚.人家也有贼短的代码,人家很牛逼.

算法分两大阶段(细分为 8 steps, 在程序注释中所示)

  1. 设置第0行,第0列以及col0,让它们正确表达所对应行、列的状态;
  2. 依据上述状态将对应行、列置0.

第一第二阶段均需注意顺序,如程序中注释所示.

第二阶段清0时,如下图:

  1. ^ 表示第0行或第0列的元素;
  2. * 表示非0行,非0列元素
  3. 依据状态置0时,先清非第0行和第0列的元素,那就是 * 表示的那帮货!
  4. 再清第0行,最后依据col0清第0列.
  5. ^ ^ ^
  6. ^ * *
  7. ^ * *

有意思的是:col0展示了她的重要地位,她已跳出三界外,不在五行中.整个程序从她而起,最后又由她而终!

自己代码和注释:

\(O(n^2)\) time, \(O(1)\) space.

  1. void setZeroes(vector<vector<int>>& A) {
  2. // 程序分为8个step, 各step顺序不能颠倒
  3. // 终极目的是避免记录状态的第0行和第0列被误写
  4. int m = A.size(), n = A[0].size(), col0 = 1;
  5. // step1. 若第0列有0, col0 = 0
  6. for (int i = 0; i < m; i++)
  7. if (A[i][0] == 0) {
  8. col0 = 0;
  9. break;
  10. }
  11. // step2. 若第0行有0, row0 = 0
  12. for (int j = 0; j < n; j++)
  13. if (A[0][j] == 0) {
  14. A[0][0] = 0;
  15. break;
  16. }
  17. // step3. 依次检查除第0列以外的其他列j,若那列有0,则A[0][j]=0
  18. for (int j = 1; j < n; j++)
  19. for (int i = 0; i < m; i++)
  20. if (A[i][j] == 0) {
  21. A[0][j] = 0;
  22. break;
  23. }
  24. // step4. 依次检查除第0行以外的其他行i,若那行有0,则A[i][0]=0
  25. for (int i = 1; i < m; i++)
  26. for (int j = 0; j < n; j++)
  27. if (A[i][j] == 0) {
  28. A[i][0] = 0;
  29. break;
  30. }
  31. // 阶段性胜利:到此为止,我们把第0行与第0列,以及col0的 state 设置完成了
  32. // 接下来,我们要根据上述状态,将对应元素设置为0
  33. // step5. 依据第0列,修改第1~(m-1)行元素为0(注意:不改第0行,因为那里存着state)
  34. for (int i = 1; i < m; i++)
  35. if (A[i][0] == 0)
  36. for (int j = 1; j < n; j++)
  37. A[i][j] = 0;
  38. // step6. 依据第0行,修改第1~(n-1)列元素为0(注意:不改第0列,因为那里存着state)
  39. for (int j = 1; j < n; j++)
  40. if (A[0][j] == 0)
  41. for (int i = 1; i < m; i++)
  42. A[i][j] = 0;
  43. // step7. 按照A[0][0], 修改第0行元素为0
  44. if (A[0][0] == 0)
  45. for (int j = 1; j < n; j++)
  46. A[0][j] = 0;
  47. // step8. 按照col0, 修改第0列元素为0, 再次强调, 以上次序不能调换
  48. if (col0 == 0)
  49. for (int i = 0; i < m; i++) //此时需将A[0][0]元素包含在内
  50. A[i][0] = 0;
  51. }

73. Set Matrix Zeroes(中等)的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. Leetcode#73 Set Matrix Zeroes

    原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) ...

  4. [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. Follow ...

  5. leetcode[73] Set Matrix Zeroes 将矩阵置零

    给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...

  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. 73. Set Matrix Zeroes 把矩阵同一行列的元素都改成0

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

随机推荐

  1. GIT入门笔记(13)- GUI GIT

  2. EasyUI combobox下拉多选框的实现

    combobox实现下拉列表多选, 效果如下

  3. SpringBoot(三):springboot启动参数

    springboot默认启动入口函数是支持接收参数,并且在整个应用程序内部也可以获取到这些参数,并且如果传递的参数是一些内部定义的参数将会被映射到springboot内部配置项,从而达到配置效果. s ...

  4. spark2.1:读取hive中存储的多元组(string,double)失败

    这两天和同事一起在想着如何把一个表的记录减少,表记录包含了:objectid(主小区信息),gridid(归属栅格),height(高度),rsrp(主小区rsrp),n_objectid(邻区),n ...

  5. 原生JS实现几个常用DOM操作API

    原生实现jQuery的sibling方法 <body> <span>我是span标签</span> <div>我是一个div</div> & ...

  6. hue集成hbase出现TSocket read 0 bytes

    解决办法:修改hbase的配置文件 添加以下配置 https://stackoverflow.com/questions/20415493/api-error-tsocket-read-0-bytes ...

  7. C++ namespace的作用

    namespace:命名空间或者叫名字空间,传统的c++只有一个全局的namespace,但是由于现在的程序规模越来越大,程序的分工越来越细,全局作用域就变得越来越拥挤,每个人都可能使用相同的名字来实 ...

  8. [LeetCode] Self Dividing Numbers 自整除数字

    A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is ...

  9. [LeetCode] Distribute Candies 分糖果

    Given an integer array with even length, where different numbers in this array represent different k ...

  10. css中单位px,em,rem和vh/vw的理解

    >px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的. em是相对长度单位.相对于当前对象内文本的字体尺寸.如当前对行内文本的字体尺寸未被人为设置,则相对于浏览器的默认 ...