当前位置:网站首页>面试题 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的行和列,然后进行遍历那个那两个存储存储行和列的数组,这种方法就比较简单了
边栏推荐
- Which securities company is better to open an account? Is online account opening safe
- El tree combined with El table, tree adding and modifying operations
- Sorting and sharing of selected papers, systems and applications related to the most comprehensive mixed expert (MOE) model in history
- QT—双缓冲绘图
- Hash table
- Golang面试整理 三 简历如何书写
- vim 从嫌弃到依赖(23)——最后的闲扯
- EhLib 数据库记录的下拉选择
- 电话加密,中间4为****代替
- 做BI开发,为什么一定要熟悉行业和企业业务?
猜你喜欢

QT - plot other problems

Analysis of maker education technology in the Internet Era

VS2019 C# release下断点调试
![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]

TCP shakes hands three times and waves four times. Do you really understand?

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

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

Operation of adding material schedule in SolidWorks drawing

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

How was MP3 born?
随机推荐
Why does invariant mode improve performance
Flutter 返回按钮的监听
改善机器视觉系统的方法
Interpreting the development of various intelligent organizations in maker Education
GTEST from ignorance to proficiency (4) how to write unit tests with GTEST
关系型数据库
什么是商业智能(BI),就看这篇文章足够了
Liu Jincheng won the 2022 China e-commerce industry innovation Figure Award
[public class preview]: basis and practice of video quality evaluation
Delphi soap WebService server-side multiple soapdatamodules implement the same interface method, interface inheritance
Jerry's ad series MIDI function description [chapter]
How to use concurrentlinkedqueue as a cache queue
How was MP3 born?
Numpy vstack and column_ stack
redis RDB AOF
OMS系统实战的三两事
Kubedm initialization error: [error cri]: container runtime is not running
[weekly translation go] how to code in go series articles are online!!
解析互联网时代的创客教育技术
gtest从一无所知到熟练使用(3)什么是test suite和test case