leetcode76set-matrix-zeroes
题目描述
Follow up: Did you use extra space?
A straight forward solution using O(m n) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
const int row=matrix.size();
const int col=matrix[0].size();
bool row_flag=false,col_flag=false;
for (int i=0;i<row ;i++)
if (0==matrix [i][0])
{
col_flag=true;
break;
}
for (int i=0;i<col;i++)
if (0==matrix[0][i])
{
row_flag=true;
break;
}
for (int i=1;i<row;i++)
for (int j=1;j<col;j++)
if (0==matrix[i][j]){
matrix[i][0]=0;
matrix[0][j]=0;
}
for (int i=1;i<row;i++){
for (int j=1;j<col;j++){
if (0==matrix[i][0] || matrix [0][j]==0)
matrix[i][j]=0;
}
}
if (row_flag)
for (int i=0;i<col;i++)
matrix[0][i]=0;
if (col_flag)
for (int i=0;i<row;i++)
matrix[i][0]=0;
}
};
最新文章
- POJ 1743 Musical Theme 二分+后缀数组
- Linux 命令——grep | 正则表达式
- Android框架之AndroidAnnotations实战
- chrome浏览器不允许记忆登录账户的方法
- (7)基本工作流(使用AndroidStudio编辑Cocos项目)
- CentOS6.4 配置Nload监控网卡流量
- WinDump使用方法
- 单元测试之Fixture
- php设计模式--简单介绍
- 获取BDC 消息文本的2种方式
- 23.Hibernate-基础.md
- Javascript和JQuery函数定义方式
- 企业安全建设之搭建开源SIEM平台
- 笔记 freemark list标签迭代Map<;Map<;String,Object>;集合排序问题
- 关于jquery 取值,赋值常用控件的问题
- pandas生成时间列表(某段连续时间或者固定间隔时间段)
- MAVEN教程--01安装|创建|解释
- ssm项目快速搭建(注解)
- js随笔--关于数组
- 【LOJ】#2174. 「FJOI2016」神秘数
热门文章
- Mac安装mongodb并启动
- 基于python常用排序与查找
- 什么是 C 和 C ++ 标准库?学编程的你应该知道这些知识!
- Mysql数据库分布式事务XA详解
- docker查看ip
- PHP获取当前毫秒级别时间戳
- 使用原生js模拟jQuery选择器,实现new方法,兼容ie5
- 【服务总线 Azure Service Bus】ServiceBus 队列中死信(DLQ - Dead Letter Queue)问题
- python的部分GUI模块简介tkinter、pyqt5(Qt Designer)
- poj1655 Balancing Act (dp? dfs?)