leetcode 之Set Matrix Zeroes(10)

 设置两个布尔数组,记录行和列是否存在0。需要注意的是如何将行或列设为0.

void setZeros(vector<vector<int>> &matrix)
      {
          int m = matrix.size();
          int n = matrix[0].size();

          vector<bool>row(m, false);
          vector<bool>col(n, false);

          for (int i = 0; i < m;i++)
          for (int j = 0; j < n; j++)
          {
              if (matrix[i][j] == 0)
                  row[i] = col[j] = true;
          }

          for (int i = 0; i < m; i++)
          {
              if (row[i] == true)
                  fill(&matrix[i][0], &matrix[i][0] + n, 0);
          }
          for (int j = 0; j < n; j++)
          {
              if (col[j] == true)
              {
                  for (int i = 0; i < m; i++)
                      matrix[i][j] = 0;
              }
          }
View Code

 

posted @ 2016-05-16 14:21  牧马人夏峥  阅读(118)  评论(0编辑  收藏  举报