码迷,mamicode.com
首页 > 其他好文 > 详细

73. Set Matrix Zeroes

时间:2017-12-06 23:56:26      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:set   als   ace   遍历数组   设置   style   span   void   矩阵   

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 };

 

 

73. Set Matrix Zeroes

标签:set   als   ace   遍历数组   设置   style   span   void   矩阵   

原文地址:http://www.cnblogs.com/sword-/p/7995158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!