leetcode 73. Set Matrix Zeroes
2016-04-16 21:36
344 查看
题意
将矩阵中为0点它所在行和列都设置为0题解
用两个集合保存需要设置的行和列。时间复杂度为O(m * n)代码
class Solution { public: void setZeroes(vector<vector<int>>& matrix) { set<int> rows, cols; int m = matrix.size(), n = matrix[0].size(); for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if(matrix[i][j] == 0) rows.insert(i), cols.insert(j); } } for(set<int>::iterator i = rows.begin(); i != rows.end(); i++) { for(int j = 0; j < n; j++) { matrix[*i][j] = 0; } } for(set<int>::iterator i = cols.begin(); i != cols.end(); i++) { for(int j = 0; j < m; j++) { matrix[j][*i] = 0; } } } };
相关文章推荐
- Linux下fdisk磁盘分区
- 百度地图开发之定位
- 安装java之后,找不到tools.jar和dt.jar(dos下javac命令无法执行)
- python-MySQL学习笔记-第四章-利用Connector/Python来查询数据
- MapReduce实例-倒排索引
- struts2入门
- 动态规划(11)-最长回文子序列
- Integer源码浅析
- 用户登录注册案例分析
- 创建线程的三种方法
- Linux下DNS服务器搭建
- android:visibility中"invisible"与"gone"对android:layout_weight的影响
- android MVC与MVP
- HDU 2066 一个人的旅行
- 【hdu1867】A + B for you again——KMP
- Java中字符串两种等于的方法的对比
- vb6.0陈伟教学视频总结
- Optimizing Oracle RAC
- iOS 去掉UITableView风格为group时候的最顶部的空白距离
- minAreaRect-OpenCV