zoukankan      html  css  js  c++  java
  • 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.

    给定一个m × n矩阵,如果一个元素为0,则将其整个行和列设置为0。

    (1)思想1:用两个一维数组:flag_row[row] 和flag_column[column] 来保存行和列是否有0,如果 matrix[i][j]=0,则令 flag_row[i]=true,falg_column[j]=true,然后再次遍历数组,查询对应的行和列是否为true,如果是,则修改值,代码如下:

     1 class Solution {
     2 public:
     3     void setZeroes(vector<vector<int>>& matrix) {
     4         int row=matrix.size();
     5         int column=matrix[0].size();
     6         int flag_row[row]={false};
     7         int flag_column[column]={false};
     8         for(int i=0;i<row;i++)
     9         {
    10             for(int j=0;j<column;j++)
    11             {
    12                 if(matrix[i][j]==0)
    13                 {
    14                     flag_row[i]=true;
    15                     flag_column[j]=true;
    16                 }
    17             }
    18         }
    19         for(int i=0;i<row;i++)
    20         {
    21             for(int j=0;j<column;j++)
    22             {
    23                 if(flag_row[i] || flag_column[j])
    24                     matrix[i][j]=0;
    25             }
    26         }
    27     }
    28 };

     

  • 相关阅读:
    人事面试测试篇25
    人事面试测试篇19
    人事面试测试篇24
    人事面试测试篇20
    人事面试测试篇18
    人事面试测试篇23
    人事面试测试篇21
    人事面试测试篇17
    明天……
    重装系统
  • 原文地址:https://www.cnblogs.com/sword-/p/7995158.html
Copyright © 2011-2022 走看看