当前位置:网站首页>面试题 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
边栏推荐
- Transplant QT system for i.mx6ull development board - cross compile QT code
- Thoroughly understand kubernetes scheduling framework and plug-ins
- Matlab drawing - points and vectors: method and source code of vector addition and subtraction
- 总投资近16亿元!乾照光电VCSEL、高端LED芯片项目正式开工
- Flutter--密码登录注册界面
- Interpretation of new features | the restriction of MySQL 8.0 on gtid is lifted
- Monitor mouse sideslip (adapt to mobile terminal)
- 【C语言】文件操作
- Support Wu xiongang! Arm Chinese management sent an open letter: shocked and angry at the unwarranted allegations!
- Nokia announces cooperation with Broadcom to develop 5g chip
猜你喜欢

Lua进阶

Matlab drawing - points and vectors: method and source code of vector addition and subtraction

LeetCode 2341. 数组能形成多少数对

Shutter -- password login registration interface

Redefine analysis - release of eventbridge real-time event analysis platform

Summary of common shortcut keys in idea

Codeforces summer training weekly (7.21~7.27)

8000 word explanation of OBSA principle and application practice

I want to get 20K after 3 years of experience, but I haven't got it for half a month?

Principle of logistic regression
随机推荐
糟糕程序员的20个坏习惯
C language main function transfer parameters
JG-数据重置(wd)
Cesium add inundation analysis measurement area
Byte monthly salary 28K, share a wave of my automation testing experience
总投资近16亿元!乾照光电VCSEL、高端LED芯片项目正式开工
In April, global smartphone shipments fell 41% year-on-year, and Huawei surpassed Samsung to become the world's first for the first time
Jingfang Technology: ASML, a lithography machine manufacturer, is one of the main customers of Anterion company, which participated in the merger and acquisition of the company
激光器芯片厂商陕西源杰半导体获广发证券、中信证券等8家投资机构入股
吴雄昂发内部信:Arm的指控是莫须有的,现有成果不允许有任何损害!
Thoroughly understand kubernetes scheduling framework and plug-ins
【游戏】任天堂Nintendo Switch超详细购买/使用指南以及注意事项(根据自己使用持续更新中...)
Detailed explanation of retinanet network structure
晶方科技:光刻机厂商ASML为公司参与并购的Anteryon公司的最主要客户之一
现货白银如何计算盈亏
20 bad habits of bad programmers
Day 013 one dimensional array exercise
System clock failure of database fault tolerance
Fluent call interface UI
Adding custom dynamic arts and Sciences to cesium