当前位置:网站首页>面试题 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的行和列,然后进行遍历那个那两个存储存储行和列的数组,这种方法就比较简单了
边栏推荐
- MP3是如何诞生的?
- 刘锦程荣获2022年度中国电商行业创新人物奖
- Cloudcompare & open3d DBSCAN clustering (non plug-in)
- Delphi soap WebService server-side multiple soapdatamodules implement the same interface method, interface inheritance
- 面试官:说说XSS攻击是什么?
- Maidong Internet won the bid of Beijing life insurance
- Le module minidom écrit et analyse XML
- Acwing 2022 daily question
- Analysis of maker education technology in the Internet Era
- minidom 模块写入和解析 XML
猜你喜欢
WGCNA analysis basic tutorial summary
Jerry's ad series MIDI function description [chapter]
Application practice | Shuhai supply chain construction of data center based on Apache Doris
如何使用ConcurrentLinkedQueue做一个缓存队列
案例分享|金融业数据运营运维一体化建设
MP3是如何诞生的?
改善机器视觉系统的方法
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
[leetcode] 17. Letter combination of telephone number
Interpreting the development of various intelligent organizations in maker Education
随机推荐
Kubedm initialization error: [error cri]: container runtime is not running
TCP协议三次握手过程
Use of redis publish subscription
什么是商业智能(BI),就看这篇文章足够了
QT - plot other problems
Word文档中标题前面的黑点如何去掉
HDU - 1078 FatMouse and Cheese(记忆化搜索DP)
Analysis of maker education technology in the Internet Era
创客思维在高等教育中的启迪作用
bizchart+slider实现分组柱状图
VS2019 C# release下断点调试
Interpreting the development of various intelligent organizations in maker Education
new IntersectionObserver 使用笔记
历史最全混合专家(MOE)模型相关精选论文、系统、应用整理分享
Lambdaquerywrapper usage
Exclusive interview of open source summer | new committer Xie Qijun of Apache iotdb community
Redis 排查大 key 的3种方法,优化必备
哈希表(Hash Tabel)
At the right time, the Guangzhou station of the city chain science and Technology Strategy Summit was successfully held
做BI开发,为什么一定要熟悉行业和企业业务?