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

Example 1:

  1. Input:
  2. [
  3.   [1,1,1],
  4.   [1,0,1],
  5.   [1,1,1]
  6. ]
  7. Output:
  8. [
  9.   [1,0,1],
  10.   [0,0,0],
  11.   [1,0,1]
  12. ]

Example 2:

  1. Input:
  2. [
  3.   [0,1,2,0],
  4.   [3,4,5,2],
  5.   [1,3,1,5]
  6. ]
  7. Output:
  8. [
  9.   [0,0,0,0],
  10.   [0,4,5,0],
  11.   [0,3,1,0]
  12. ]

Follow up:

  • 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?
 

给一个m x n的矩阵,如果一个元素是0,就把它所在的行和列都设成0,用in place做。

解法1: 新建一个矩阵,然后一行一行的扫,只要有0,就将新建的矩阵的对应行全赋0,行扫完再扫列,然后把更新完的矩阵赋给matrix。空间复杂度为O(mn)。

解法2: 用一个长度为m的一维数组记录各行中是否有0,用一个长度为n的一维数组记录各列中是否有0,最后直接更新matrix数组即可。空间复杂度为O(m+n),

解法3: 这道题要求用常数级空间复杂度O(1),不能新建数组,就用原数组的第一行第一列来记录各行各列是否有0.

- 先扫描第一行第一列,如果有0,则将各自的flag设置为true
- 然后扫描除去第一行第一列的整个数组,如果有0,则将对应的第一行和第一列的数字赋0
- 再次遍历除去第一行第一列的整个数组,如果对应的第一行和第一列的数字有一个为0,则将当前值赋0
- 最后根据第一行第一列的flag来更新第一行第一列

时间复杂度是O(m*n), 三种方法都一样,需要进行两次扫描,一次确定行列置0情况,一次对矩阵进行实际的置0操作。

Java:

  1. void setZeroes(vector<vector<int> > &matrix) {
  2. int col0 = 1, rows = matrix.size(), cols = matrix[0].size();
  3.  
  4. for (int i = 0; i < rows; i++) {
  5. if (matrix[i][0] == 0) col0 = 0;
  6. for (int j = 1; j < cols; j++)
  7. if (matrix[i][j] == 0)
  8. matrix[i][0] = matrix[0][j] = 0;
  9. }
  10.  
  11. for (int i = rows - 1; i >= 0; i--) {
  12. for (int j = cols - 1; j >= 1; j--)
  13. if (matrix[i][0] == 0 || matrix[0][j] == 0)
  14. matrix[i][j] = 0;
  15. if (col0 == 0) matrix[i][0] = 0;
  16. }
  17. }  

Java:

  1. public void setZeroes(int[][] matrix) {
  2. boolean fr = false,fc = false;
  3. for(int i = 0; i < matrix.length; i++) {
  4. for(int j = 0; j < matrix[0].length; j++) {
  5. if(matrix[i][j] == 0) {
  6. if(i == 0) fr = true;
  7. if(j == 0) fc = true;
  8. matrix[0][j] = 0;
  9. matrix[i][0] = 0;
  10. }
  11. }
  12. }
  13. for(int i = 1; i < matrix.length; i++) {
  14. for(int j = 1; j < matrix[0].length; j++) {
  15. if(matrix[i][0] == 0 || matrix[0][j] == 0) {
  16. matrix[i][j] = 0;
  17. }
  18. }
  19. }
  20. if(fr) {
  21. for(int j = 0; j < matrix[0].length; j++) {
  22. matrix[0][j] = 0;
  23. }
  24. }
  25. if(fc) {
  26. for(int i = 0; i < matrix.length; i++) {
  27. matrix[i][0] = 0;
  28. }
  29. }
  30.  
  31. }  

Java:

  1. public class Solution {
  2. public void setZeroes(int[][] matrix) {
  3. boolean firstRowZero = false;
  4. boolean firstColumnZero = false;
  5.  
  6. //set first row and column zero or not
  7. for(int i=0; i<matrix.length; i++){
  8. if(matrix[i][0] == 0){
  9. firstColumnZero = true;
  10. break;
  11. }
  12. }
  13.  
  14. for(int i=0; i<matrix[0].length; i++){
  15. if(matrix[0][i] == 0){
  16. firstRowZero = true;
  17. break;
  18. }
  19. }
  20.  
  21. //mark zeros on first row and column
  22. for(int i=1; i<matrix.length; i++){
  23. for(int j=1; j<matrix[0].length; j++){
  24. if(matrix[i][j] == 0){
  25. matrix[i][0] = 0;
  26. matrix[0][j] = 0;
  27. }
  28. }
  29. }
  30.  
  31. //use mark to set elements
  32. for(int i=1; i<matrix.length; i++){
  33. for(int j=1; j<matrix[0].length; j++){
  34. if(matrix[i][0] == 0 || matrix[0][j] == 0){
  35. matrix[i][j] = 0;
  36. }
  37. }
  38. }
  39.  
  40. //set first column and row
  41. if(firstColumnZero){
  42. for(int i=0; i<matrix.length; i++)
  43. matrix[i][0] = 0;
  44. }
  45.  
  46. if(firstRowZero){
  47. for(int i=0; i<matrix[0].length; i++)
  48. matrix[0][i] = 0;
  49. }
  50.  
  51. }
  52. }  

Java:

  1. public void setZeroes(int[][] matrix) {
  2. if(matrix==null || matrix.length==0 || matrix[0].length==0)
  3. return;
  4. boolean rowFlag = false;
  5. boolean colFlag = false;
  6. for(int i=0;i<matrix.length;i++)
  7. {
  8. if(matrix[i][0]==0)
  9. {
  10. colFlag = true;
  11. break;
  12. }
  13. }
  14. for(int i=0;i<matrix[0].length;i++)
  15. {
  16. if(matrix[0][i]==0)
  17. {
  18. rowFlag = true;
  19. break;
  20. }
  21. }
  22. for(int i=1;i<matrix.length;i++)
  23. {
  24. for(int j=1;j<matrix[0].length;j++)
  25. {
  26. if(matrix[i][j]==0)
  27. {
  28. matrix[i][0] = 0;
  29. matrix[0][j] = 0;
  30. }
  31. }
  32. }
  33. for(int i=1;i<matrix.length;i++)
  34. {
  35. for(int j=1;j<matrix[0].length;j++)
  36. {
  37. if(matrix[i][0]==0 || matrix[0][j]==0)
  38. matrix[i][j] = 0;
  39. }
  40. }
  41. if(colFlag)
  42. {
  43. for(int i=0;i<matrix.length;i++)
  44. {
  45. matrix[i][0] = 0;
  46. }
  47. }
  48. if(rowFlag)
  49. {
  50. for(int i=0;i<matrix[0].length;i++)
  51. {
  52. matrix[0][i] = 0;
  53. }
  54. }
  55. }   

Python:

  1. class Solution:
  2. # @param matrix, a list of lists of integers
  3. # RETURN NOTHING, MODIFY matrix IN PLACE.
  4. def setZeroes(self, matrix):
  5. first_col = reduce(lambda acc, i: acc or matrix[i][0] == 0, xrange(len(matrix)), False)
  6. first_row = reduce(lambda acc, j: acc or matrix[0][j] == 0, xrange(len(matrix[0])), False)
  7.  
  8. for i in xrange(1, len(matrix)):
  9. for j in xrange(1, len(matrix[0])):
  10. if matrix[i][j] == 0:
  11. matrix[i][0], matrix[0][j] = 0, 0
  12.  
  13. for i in xrange(1, len(matrix)):
  14. for j in xrange(1, len(matrix[0])):
  15. if matrix[i][0] == 0 or matrix[0][j] == 0:
  16. matrix[i][j] = 0
  17.  
  18. if first_col:
  19. for i in xrange(len(matrix)):
  20. matrix[i][0] = 0
  21.  
  22. if first_row:
  23. for j in xrange(len(matrix[0])):
  24. matrix[0][j] = 0  

Python:

  1. class Solution:
  2. # @param {integer[][]} matrix
  3. # @return {void} Do not return anything, modify matrix in-place instead.
  4. def setZeroes(self, matrix):
  5. m = len(matrix)
  6. if m == 0:
  7. return
  8. n = len(matrix[0])
  9.  
  10. row_zero = False
  11. for i in range(m):
  12. if matrix[i][0] == 0:
  13. row_zero = True
  14. col_zero = False
  15. for j in range(n):
  16. if matrix[0][j] == 0:
  17. col_zero = True
  18.  
  19. for i in range(1, m):
  20. for j in range(1, n):
  21. if matrix[i][j] == 0:
  22. matrix[i][0] = 0
  23. matrix[0][j] = 0
  24.  
  25. for i in range(1, m):
  26. if matrix[i][0] == 0:
  27. for j in range(1, n):
  28. matrix[i][j] = 0
  29.  
  30. for j in range(1, n):
  31. if matrix[0][j] == 0:
  32. for i in range(1, m):
  33. matrix[i][j] = 0
  34.  
  35. if col_zero:
  36. for j in range(n):
  37. matrix[0][j] = 0
  38. if row_zero:
  39. for i in range(m):
  40. matrix[i][0] = 0

C++:

  1. class Solution {
  2. public:
  3. void setZeroes(vector<vector<int> > &matrix) {
  4. if (matrix.empty() || matrix[0].empty()) return;
  5. int m = matrix.size(), n = matrix[0].size();
  6. bool rowZero = false, colZero = false;
  7. for (int i = 0; i < m; ++i) {
  8. if (matrix[i][0] == 0) colZero = true;
  9. }
  10. for (int i = 0; i < n; ++i) {
  11. if (matrix[0][i] == 0) rowZero = true;
  12. }
  13. for (int i = 1; i < m; ++i) {
  14. for (int j = 1; j < n; ++j) {
  15. if (matrix[i][j] == 0) {
  16. matrix[0][j] = 0;
  17. matrix[i][0] = 0;
  18. }
  19. }
  20. }
  21. for (int i = 1; i < m; ++i) {
  22. for (int j = 1; j < n; ++j) {
  23. if (matrix[0][j] == 0 || matrix[i][0] == 0) {
  24. matrix[i][j] = 0;
  25. }
  26. }
  27. }
  28. if (rowZero) {
  29. for (int i = 0; i < n; ++i) matrix[0][i] = 0;
  30. }
  31. if (colZero) {
  32. for (int i = 0; i < m; ++i) matrix[i][0] = 0;
  33. }
  34. }
  35. };

  

All LeetCode Questions List 题目汇总

[LeetCode] 73. Set Matrix Zeroes 矩阵赋零的更多相关文章

  1. [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零

    1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are ...

  2. [LeetCode] 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 ...

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

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

  4. [Leetcode] 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 ...

  5. 073 Set Matrix Zeroes 矩阵置零

    给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...

  6. [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 ...

  7. Leetcode73. Set Matrix Zeroes矩阵置零

    给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   [1,0,1],   [1,1,1] ] 输 ...

  8. Leetcode#73 Set Matrix Zeroes

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

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

随机推荐

  1. jquery 内容筛选选择器

    基本筛选选择器针对的都是元素DOM节点,如果我们要通过内容来过滤,jQuery也提供了一组内容筛选选择器,当然其规则也会体现在它所包含的子元素或者文本内容上 注意事项: :contains与:has都 ...

  2. Android Binder机制彻底梳理一

    Binder架构图: 先来瞅一下它的整体架构图: 其中粉红部分是上层的Binder,而蓝色的则是下层的Binder,很显然上层的是依赖于下层的. 什么是Binder[有个大概了解]? 这里从几个层面来 ...

  3. Nastya Hasn't Written a Legend(Codeforces Round #546 (Div. 2)E+线段树)

    题目链接 传送门 题面 题意 给你一个\(a\)数组和一个\(k\)数组,进行\(q\)次操作,操作分为两种: 将\(a_i\)增加\(x\),此时如果\(a_{i+1}<a_i+k_i\),那 ...

  4. git免密

    免账号密码输入 git clone https://lichuanfa%40gitcloud.com.cn:lcf13870752164@git.c.citic/Citic-Data/bigdata_ ...

  5. 读取yaml文件小方法

    def read_inf(inf_path): '''读取指定路径配置文件''' try: import yaml fr = open(inf_path) fy = yaml.load(fr) fr. ...

  6. Spark API--Spark 分区

    一.分区的概念 分区是RDD内部并行计算的一个计算单元,RDD的数据集在逻辑上被划分为多个分片,每一个分片称为分区,分区的格式决定了并行计算的粒度,而每个分区的数值计算都是在一个任务中进行的,因此任务 ...

  7. 基于Helm和Operator的K8S应用管理

    https://blog.csdn.net/RancherLabs/article/details/79483013 大家好,今天我们分享的内容是基于Helm和Operator的K8S应用管理. 我们 ...

  8. 文件操作时:xreadlines和readlines的区别?

    二者使用时相同,但返回类型不同,xreadlines返回的是一个生成器,readlines返回的是list

  9. Stability Analysis of Algorithms

    算法(Algorithm)是指用来操作数据.解决程序问题的一组方法.对于同一个问题,使用不同的算法,也许最终得到的结果是一样的,比如排序就有前面的十大经典排序和几种奇葩排序,虽然结果相同,但在过程中消 ...

  10. NTSTATUS代码摘录

    00000000 STATUS_SUCCESS00000000 STATUS_WAIT_000000001 STATUS_WAIT_100000002 STATUS_WAIT_200000003 ST ...