当前位置:网站首页>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
边栏推荐
- Analyzing the maker space contained in steam Education
- 【活动早知道】LiveVideoStack近期活动一览
- 做BI开发,为什么一定要熟悉行业和企业业务?
- 电话加密,中间4为****代替
- [early knowledge of activities] list of recent activities of livevideostack
- gtest从一无所知到熟练使用(4)如何用gtest写单元测试
- 2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
- gtest从一无所知到熟练运用(1)gtest安装
- 面试题 01.08. 零矩阵
- 类方法和类变量的使用
猜你喜欢
el-tree结合el-table,树形添加修改操作
Daily question-leetcode556-next larger element iii-string-double pointer-next_ permutation
如何使用ConcurrentLinkedQueue做一个缓存队列
做BI开发,为什么一定要熟悉行业和企业业务?
什么是商业智能(BI),就看这篇文章足够了
【C語言】符號的深度理解
How was MP3 born?
[early knowledge of activities] list of recent activities of livevideostack
GTEST from ignorance to proficiency (3) what are test suite and test case
MP3是如何诞生的?
随机推荐
Compréhension approfondie du symbole [langue C]
挖财学院股票开户安全吗?开户只能在挖财开户嘛?
开户哪家券商比较好?网上开户安全吗
保证接口数据安全的10种方案
The drawing method of side-by-side diagram, multi row and multi column
面试官:说说XSS攻击是什么?
Why do you have to be familiar with industry and enterprise business when doing Bi development?
做BI开发,为什么一定要熟悉行业和企业业务?
bizchart+slider实现分组柱状图
How much is the minimum stock account opening commission? Is it safe to open an account online
PostgreSQL基本结构——表
How was MP3 born?
How is the entered query SQL statement executed?
Bookmark
Golang面试整理 三 简历如何书写
Daily question -leetcode1200- minimum absolute difference - array - sort
Which securities company has the lowest Commission for opening an account online? I want to open an account. Is it safe to open an account online
New intersectionobserver usage notes
HUAWEI nova 10系列发布 华为应用市场筑牢应用安全防火墙
并列图的画法,多排多列