leetcode 073 Set Matrix Zeroes
2016-05-02 19:27
393 查看
Given a m x n matrix,
if an element is 0, set its entire row and column to 0. Do it in place.
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int rows = matrix.size(), cols = 0;
if(rows == 0) return ;
cols = matrix[0].size();
set<int> rowNum;
set<int> colNum;
for(int i=0; i < rows; i++) {
for(int j=0; j < cols; j++) {
if(matrix[i][j] == 0) {
rowNum.insert(i);
colNum.insert(j);
}
}
}
set<int>::iterator itr;
for(itr=rowNum.begin(); itr!=rowNum.end(); itr++) {
int temp = *itr;
for(int i = 0; i < cols; i++)
matrix[temp][i] = 0;
}
for(itr=colNum.begin(); itr!=colNum.end(); itr++) {
int temp = *itr;
for(int i=0; i < rows; i++) {
matrix[i][temp] = 0;
}
}
}
};
if an element is 0, set its entire row and column to 0. Do it in place.
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int rows = matrix.size(), cols = 0;
if(rows == 0) return ;
cols = matrix[0].size();
set<int> rowNum;
set<int> colNum;
for(int i=0; i < rows; i++) {
for(int j=0; j < cols; j++) {
if(matrix[i][j] == 0) {
rowNum.insert(i);
colNum.insert(j);
}
}
}
set<int>::iterator itr;
for(itr=rowNum.begin(); itr!=rowNum.end(); itr++) {
int temp = *itr;
for(int i = 0; i < cols; i++)
matrix[temp][i] = 0;
}
for(itr=colNum.begin(); itr!=colNum.end(); itr++) {
int temp = *itr;
for(int i=0; i < rows; i++) {
matrix[i][temp] = 0;
}
}
}
};
相关文章推荐
- XML的解析
- 快速点击两次退出应用
- Just a Hook---hdu1698(线段树---区间处理)
- iOS App icon、启动页、图标规范
- 【操作系统】OS的整体框架
- hiho#1050 : 树中的最长路
- c语言学生信息管理系统学习感想
- 如何成为一个牛逼的C/C++程序员?
- poj 1562 Oil Deposits(dfs)
- jsp集成markdown编辑器
- WebService核心文件【server-config.wsdd】详解及调用示例
- SharedPreference.Editor 的 apply() 和 commit() 的区别
- 正则表达式——Regular Expression
- 优先队列
- Android自定义属性
- C# winform中文本框限制只能输入数字和字母,退格键
- 完美长方体是不可能存在的 第三章
- hdu 5676 ztr loves lucky numbers(dfs+离线)
- awk对数据排序实例
- AppCode的使用