题目链接:https://oj.leetcode.com/problems/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.
一个个找肯定会超时,我们可以分别用一个行向量和一个列向量进行维护。这样O(m*n) 能出来
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
bool row[matrix.size()];
bool col[matrix[0].size()];
memset(row,false,sizeof(row));
memset(col,false,sizeof(col));
for(int i=0;i<matrix.size();i++)
for(int j=0;j<matrix[0].size();j++)
{
if(matrix[i][j]==0)
{
row[i]=true;
col[j]=true;
}
}
for(int i=0;i<matrix.size();i++)
for(int j=0;j<matrix[0].size();j++)
{
if(row[i]==true||col[j]==true)
{
matrix[i][j]=0;
}
}
}
};学生信息管理系统,Ubuntu下Codeblocks+wxWidgets编程,布布扣,bubuko.com
学生信息管理系统,Ubuntu下Codeblocks+wxWidgets编程
原文地址:http://blog.csdn.net/u012388338/article/details/27982531