当前位置:网站首页>面试题 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的行和列,然后进行遍历那个那两个存储存储行和列的数组,这种方法就比较简单了
边栏推荐
猜你喜欢
Maya lamp modeling
Super detailed tutorial, an introduction to istio Architecture Principle and practical application
TCP三次握手,四次挥手,你真的了解吗?
[early knowledge of activities] list of recent activities of livevideostack
Sorting and sharing of selected papers, systems and applications related to the most comprehensive mixed expert (MOE) model in history
Keep on fighting! The city chain technology digital summit was grandly held in Chongqing
Analysis of maker education technology in the Internet Era
[public class preview]: basis and practice of video quality evaluation
QT—绘制其他问题
redis03——Redis的网络配置与心跳机制
随机推荐
Master the use of auto analyze in data warehouse
Use of class methods and class variables
Redis has three methods for checking big keys, which are necessary for optimization
Jerry added the process of turning off the touch module before turning it off [chapter]
VS2019 C# release下断点调试
[public class preview]: basis and practice of video quality evaluation
For MySQL= No data equal to null can be found. Solution
解决异步接口慢导致的数据错乱问题
解析steam教育中蕴含的众创空间
Daily question -leetcode1200- minimum absolute difference - array - sort
[ 每周译Go ] 《How to Code in Go》系列文章上线了!!
What is business intelligence (BI), just look at this article is enough
Application practice | Shuhai supply chain construction of data center based on Apache Doris
[optimtool.unconstrained] unconstrained optimization toolbox
Why does invariant mode improve performance
【LeetCode】17、电话号码的字母组合
Jerry's ad series MIDI function description [chapter]
PostgreSQL基本结构——表
什么是商业智能(BI),就看这篇文章足够了
面试官:说说XSS攻击是什么?