当前位置:网站首页>面试题 01.08. 零矩阵
面试题 01.08. 零矩阵
2022-07-04 21:23:00 【Mr Gao】
面试题 01.08. 零矩阵
编写一种算法,若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]
]
void dfs(int **matrix,int n ,int m,int row,int col,int *rrow,int* rcol ){
int i;
// printf("- %d %d ",col,row);
if(rrow[row]==1){
rrow[row]=0;
for(i=0;i<m;i++){
if(matrix[row][i]!=0){
matrix[row][i]=-999;
}
else{
if(rrow[row]==1||rcol[i]==1){
dfs(matrix,n , m, row, i,rrow,rcol);
}
}
}
}
if(rcol[col]==1){
rcol[col]=0;
for(i=0;i<n;i++){
if(matrix[i][col]!=0){
matrix[i][col]=-999;
}
else{
if(rrow[i]==1||rcol[col]==1){
dfs(matrix, n , m, i, col,rrow,rcol);
}
}
}
}
}
void setZeroes(int** matrix, int matrixSize, int* matrixColSize){
int n=matrixSize;
int m=matrixColSize[0];
int *rrow=(int *)malloc(sizeof(int)*n);
int *rcol=(int *)malloc(sizeof(int)*m);
int i,j;
for(i=0;i<n;i++){
rrow[i]=1;
}
for(i=0;i<m;i++){
rcol[i]=1;
}
printf("df");
for(i=0;i<n;i++){
if(rrow[i]==1){
for(j=0;j<m;j++){
if(matrix[i][j]==0){
dfs(matrix,n , m, i, j,rrow,rcol);
break;
}
}
}
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(matrix[i][j]==-999){
matrix[i][j]=0;
}
}
}
}
这里也有一种比较简单的算法,标记出现过0的行和列,然后进行遍历那个那两个存储存储行和列的数组,这种方法就比较简单了
边栏推荐
- GTEST from ignorance to skillful use (1) GTEST installation
- 类方法和类变量的使用
- Daily question-leetcode556-next larger element iii-string-double pointer-next_ permutation
- GTEST from ignorance to proficient use (2) what is test fixture
- Use of redis publish subscription
- Flutter WebView示例
- 学习突围3 - 关于精力
- GTEST from ignorance to proficiency (3) what are test suite and test case
- Interviewer: what is XSS attack?
- Methods of improving machine vision system
猜你喜欢

Enlightenment of maker thinking in Higher Education

如何使用ConcurrentLinkedQueue做一个缓存队列
![Jerry added the process of turning off the touch module before turning it off [chapter]](/img/28/5e4eb39243a0c973d0b90f76571f9b.png)
Jerry added the process of turning off the touch module before turning it off [chapter]

Operation of adding material schedule in SolidWorks drawing
![[public class preview]: basis and practice of video quality evaluation](/img/fd/42b98a08b5a0fd89c119f1d1a8fe1b.png)
[public class preview]: basis and practice of video quality evaluation

What is business intelligence (BI), just look at this article is enough

【C語言】符號的深度理解

TCP三次握手,四次挥手,你真的了解吗?
![[weekly translation go] how to code in go series articles are online!!](/img/bf/77253c87bfa1512f4b8d3d8f7ebe80.png)
[weekly translation go] how to code in go series articles are online!!

LambdaQueryWrapper用法
随机推荐
Jerry's ad series MIDI function description [chapter]
并列图的画法,多排多列
Analysis of maker education technology in the Internet Era
[public class preview]: basis and practice of video quality evaluation
MongoDB聚合操作总结
Redis has three methods for checking big keys, which are necessary for optimization
Shutter WebView example
淘宝商品评价api接口(item_review-获得淘宝商品评论API接口),天猫商品评论API接口
Jerry's ad series MIDI function description [chapter]
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
gtest从一无所知到熟练使用(4)如何用gtest写单元测试
Methods of improving machine vision system
创客思维在高等教育中的启迪作用
Interpreting the development of various intelligent organizations in maker Education
电话加密,中间4为****代替
Shutter textfield example
Numpy vstack and column_ stack
Flutter WebView示例
网上开户哪家证券公司佣金最低,我要开户,网上开户安全吗
案例分享|金融业数据运营运维一体化建设