当前位置:网站首页>面试题 01.08. 零矩阵
面试题 01.08. 零矩阵
2022-07-27 23:29:00 【欢迎来到对抗路】
题目要求:
编写一种算法,若M × N矩阵中某个元素为0,则将其所在的行与列清零。
示例 1:
输入:
[
[1,1,1],
[1,0,1],
[1,1,1]
]
输出:
[
[1,0,1],
[0,0,0],
[1,0,1]
]
示例 2:
输入:
[
[0,1,2,0],
[3,4,5,2],
[1,3,1,5]
]
输出:
[
[0,0,0,0],
[0,4,5,0],
[0,3,1,0]
]
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m, n;
m = matrix.size();
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] == 0)
{
row[i]=1;
col[j]=1;
}
}
}
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(row[i]||col[j])
matrix[i][j] = 0;
}
}
}
};
心得体会:
创建一维vector:
vector nums;//不指定长度
vector nums(n); // 指定长度为n
https://blog.csdn.net/cmy1130/article/details/88971585
边栏推荐
- LeetCode 2341. 数组能形成多少数对
- System clock failure of database fault tolerance
- Token is used in nodejs
- How the test architects of bat factories interpret various disputes of the test platform
- Transplant QT system for i.mx6ull development board - cross compile QT code
- 激光器芯片厂商陕西源杰半导体获广发证券、中信证券等8家投资机构入股
- 国产NB-IoT芯片厂商芯翼信息科技获2亿元A+轮融资
- Day 013 one dimensional array exercise
- BYD semiconductor completed the a+ round financing of 800million yuan: 30 well-known investment institutions entered the market, with a valuation of 10.2 billion yuan!
- Unity Shader入门精要学习——基础纹理
猜你喜欢

Harmonyos 3 was officially released: Hongmeng mobile phones are smooth and safe, and Hongmeng terminals are often used

Byte monthly salary 28K, share a wave of my automation testing experience

现货白银如何计算盈亏

ABAP CDS Table Function介绍与示例

Data problems can also be found if there is a space at the end of the field value of MySQL query criteria

氧气温湿度模组

Flutter--密码登录注册界面

EWM receiving ECC delivery note verification logic problem

迅为i.MX6ULL开发板Qt系统移植-交叉编译Qt代码

实现ABCD字母递增
随机推荐
Token is used in nodejs
BYD semiconductor completed the a+ round financing of 800million yuan: 30 well-known investment institutions entered the market, with a valuation of 10.2 billion yuan!
Adding custom dynamic arts and Sciences to cesium
软件测试面试题:性能测试指标有哪些?
Fabric2.4.4 version building process (complete process)
Codeforces summer training weekly (7.14~7.20)
Icml2022 | online decision transformer
Flutter--密码登录注册界面
Codeforces暑期训练周报(7.21~7.27)
Unity shader introduction Essentials - basic texture
Cesium add light sweep
idea常用的快捷键汇总
比亚迪半导体完成8亿元A+轮融资:30家知名投资机构入局,估值已达102亿元!
Thoroughly understand kubernetes scheduling framework and plug-ins
Lua advanced
The understanding of domain adaptation in transfer learning and the introduction of three technologies
伦敦银开盘时间知多少
BAT大厂测试架构师如何解读测试平台的各种争议
System clock failure of database fault tolerance
S-RPN: Sampling-balanced region proposal network for small crop pest detection