当前位置:网站首页>Interview question 01.08 Zero matrix
Interview question 01.08 Zero matrix
2022-07-04 22:02:00 【Mr Gao】
Interview questions 01.08. Zero matrix
Write an algorithm , if M × N An element in the matrix is 0, The row and column in which it is located are cleared .
Example 1:
Input :
[
[1,1,1],
[1,0,1],
[1,1,1]
]
Output :
[
[1,0,1],
[0,0,0],
[1,0,1]
]
Example 2:
Input :
[
[0,1,2,0],
[3,4,5,2],
[1,3,1,5]
]
Output :
[
[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;
}
}
}
}
There is also a relatively simple algorithm , Mark appears 0 Rows and columns of , Then traverse the two arrays that store rows and columns , This method is relatively simple
边栏推荐
猜你喜欢
Compréhension approfondie du symbole [langue C]
Analysis of maker education technology in the Internet Era
Keep on fighting! The city chain technology digital summit was grandly held in Chongqing
QT—双缓冲绘图
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
TCP protocol three times handshake process
TCP shakes hands three times and waves four times. Do you really understand?
GTEST from ignorance to proficiency (3) what are test suite and test case
How to implement Devops with automatic tools
湘江鲲鹏加入昇腾万里伙伴计划,与华为续写合作新篇章
随机推荐
类方法和类变量的使用
Bizchart+slider to realize grouping histogram
超详细教程,一文入门Istio架构原理及实战应用
Kubedm initialization error: [error cri]: container runtime is not running
Exclusive interview of open source summer | new committer Xie Qijun of Apache iotdb community
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
Jerry's ad series MIDI function description [chapter]
Go语言循环语句(第10课中3)
Compréhension approfondie du symbole [langue C]
VIM from dislike to dependence (23) -- the last gossip
文件读取写入
File read write
Jerry's ad series MIDI function description [chapter]
【LeetCode】17、电话号码的字母组合
Flutter WebView示例
bizchart+slider实现分组柱状图
如何使用ConcurrentLinkedQueue做一个缓存队列
Operation of adding material schedule in SolidWorks drawing
面试题 01.01. 判定字符是否唯一
New intersectionobserver usage notes