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

Set Matrix Zeroes

时间:2015-02-03 21:26:02      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

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

解题思路:题目很简单,可就是没想到有什么高效的方法.我遍历了两遍vector,显得有点复杂.

#include<iostream>
#include<vector>
using namespace std;
void setZeroes(vector<vector<int> > &matrix) {
	if (matrix.empty())
		return;
	vector<bool>BeZero_Row(matrix.size(), false);
	vector<bool>BeZero_Cloume(matrix[0].size(), false);
	for (int i = 0; i != matrix.size();++i){
		if (BeZero_Row[i])
			continue;
		for (int j = 0; j != matrix[i].size();++j){
			if (matrix[i][j]==0){
				BeZero_Cloume[j] = true;
				BeZero_Row[i]    = true;
			}
		}
	}
	for (int i = 0; i != matrix.size(); ++i){
		for (int j = 0; j != matrix[i].size(); ++j)
		{
			if (BeZero_Cloume[j]||BeZero_Row[i]){
				matrix[i][j] = 0;
			}
		}
	}
}


Set Matrix Zeroes

标签:

原文地址:http://blog.csdn.net/li_chihang/article/details/43456819

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