当前位置:网站首页>73. 矩阵置零(标记法)
73. 矩阵置零(标记法)
2022-06-12 17:32:00 【杨鹏伟】
题目: 矩阵置零 给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。
思路:先设置两个数组分别标记哪一行,哪一列上有为0的元素,然后遍历矩阵,将符合条件的标记为0即可。
code
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
vector<int> row(m), col(n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (!matrix[i][j]) {
row[i] = col[j] = true;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (row[i] || col[j]) {
matrix[i][j] = 0;
}
}
}
}
};边栏推荐
- How to change Golan back to the English version when it becomes the Chinese version
- Cesium抛物线方程
- Arm64 stack backtracking
- Li Kou today's question 926 Flip string to monotonic increment
- 电控学习 第二周
- To understand Devops, you must read these ten books!
- ssm常用到的依赖
- R language arma-garch-copula model and financial time series case
- 1723. minimum time to complete all work
- Crazy temporary products: super low price, big scuffle and new hope
猜你喜欢

JDBC几个坑

1.5 什么是架构师(连载)

淘宝Native研发模式的演进与思考 | DX研发模式

消息队列实战之队列优先级

Some introduction to FPC flexible circuit board design

Detailed explanation of shardingjdbc database and table

Gerrit+2 triggers Jenkins task

使用GCC的PGO(Profile-guided Optimization)优化整个系统

内核中断整体流程图

How to change Golan back to the English version when it becomes the Chinese version
随机推荐
ShardingJDBC 分库分表详解
Microsoft Office MSDT Code Execution Vulnerability (cve-2022-30190) vulnerability recurrence
Basic knowledge of stock introduction: is fixed increase good or bad?
性能优化之编译优化
Kill program errors in the cradle with spotbugs
Making nearly $90billion, Buffett's latest heavy stock exposure
Swintransformer network architecture
消息队列实战之队列优先级
Modify the configuration of the router connected to your computer. The website is 192.168.1.1
多种Qt的开发方式,你选择哪种?
How to view, modify, and delete SSH
数据库SQL操作基础
R language arma-garch-copula model and financial time series case
LCD参数解释及计算
迄今微软不同时期发布的SQL Server各版本之间的大致区别,供参考查阅
rolabelImg的安装使用
How to win the "Olympic Games" in retail technology for jd.com, the learning tyrant of the "regular examination"?
selenium元素定位
Introduction to several common functions of fiddler packet capturing (stop packet capturing, clear session window contents, filter requests, decode, set breakpoints...)
DRM 驱动 mmap 详解:(一)预备知识