当前位置:网站首页>面试题 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的行和列,然后进行遍历那个那两个存储存储行和列的数组,这种方法就比较简单了
边栏推荐
- 历史最全混合专家(MOE)模型相关精选论文、系统、应用整理分享
- [wechat applet] collaborative work and release
- Shutter WebView example
- Redis03 - network configuration and heartbeat mechanism of redis
- TCP协议三次握手过程
- flink1.13 sql基础语法(一)DDL、DML
- VS2019 C# release下断点调试
- Flutter WebView示例
- Why do you have to be familiar with industry and enterprise business when doing Bi development?
- Go语言循环语句(第10课中3)
猜你喜欢

【C語言】符號的深度理解

TCP三次握手,四次挥手,你真的了解吗?

LambdaQueryWrapper用法

解读创客教育中的各类智能化组织发展

迈动互联中标北京人寿保险

如何借助自动化工具落地DevOps

Analyzing the maker space contained in steam Education
![[C language] deep understanding of symbols](/img/4b/26cf10baa29eeff08101dcbbb673a2.png)
[C language] deep understanding of symbols

Super detailed tutorial, an introduction to istio Architecture Principle and practical application

ArcGIS 10.2.2 | solution to the failure of ArcGIS license server to start
随机推荐
Super detailed tutorial, an introduction to istio Architecture Principle and practical application
[C language] deep understanding of symbols
Compréhension approfondie du symbole [langue C]
HDU - 2859 Phalanx(DP)
2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
PostgreSQL基本结构——表
【C语言】符号的深度理解
Arcgis 10.2.2 | arcgis license server无法启动的解决办法
奋斗正当时,城链科技战略峰会广州站圆满召开
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
超详细教程,一文入门Istio架构原理及实战应用
电话加密,中间4为****代替
Jerry's ad series MIDI function description [chapter]
Minidom module writes and parses XML
Redis 排查大 key 的3种方法,优化必备
淘宝商品评价api接口(item_review-获得淘宝商品评论API接口),天猫商品评论API接口
gtest从一无所知到熟练运用(1)gtest安装
Lambdaquerywrapper usage
Three or two things about the actual combat of OMS system
Redis has three methods for checking big keys, which are necessary for optimization