当前位置:网站首页>面试题 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的行和列,然后进行遍历那个那两个存储存储行和列的数组,这种方法就比较简单了
边栏推荐
- Lambdaquerywrapper usage
- 改善机器视觉系统的方法
- 置信区间的画法
- Liu Jincheng won the 2022 China e-commerce industry innovation Figure Award
- [wechat applet] collaborative work and release
- 巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行
- Three or two things about the actual combat of OMS system
- PostgreSQL基本结构——表
- Which securities company is better to open an account? Is online account opening safe
- QT - double buffer plot
猜你喜欢

Redis has three methods for checking big keys, which are necessary for optimization

开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏

Daily question-leetcode556-next larger element iii-string-double pointer-next_ permutation

el-tree结合el-table,树形添加修改操作

How to remove the black dot in front of the title in word document

Interpreting the development of various intelligent organizations in maker Education

巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行

El tree combined with El table, tree adding and modifying operations
![[public class preview]: basis and practice of video quality evaluation](/img/fd/42b98a08b5a0fd89c119f1d1a8fe1b.png)
[public class preview]: basis and practice of video quality evaluation

从RepVgg到MobileOne,含mobileone的代码
随机推荐
redis03——Redis的网络配置与心跳机制
做BI开发,为什么一定要熟悉行业和企业业务?
AcWing 2022 每日一题
Redis pipeline
【LeetCode】17、电话号码的字母组合
Application practice | Shuhai supply chain construction of data center based on Apache Doris
ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start
Maya lamp modeling
How was MP3 born?
bizchart+slider实现分组柱状图
OMS系统实战的三两事
文件读取写入
Kubedm initialization error: [error cri]: container runtime is not running
类方法和类变量的使用
Which securities company is better to open an account? Is online account opening safe
gtest从一无所知到熟练使用(2)什么是测试夹具/装置(test fixture)
[early knowledge of activities] list of recent activities of livevideostack
超详细教程,一文入门Istio架构原理及实战应用
Jerry's ad series MIDI function description [chapter]
HDU - 1078 FatMouse and Cheese(记忆化搜索DP)