当前位置:网站首页>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
边栏推荐
- Keep on fighting! The city chain technology digital summit was grandly held in Chongqing
- From repvgg to mobileone, including mobileone code
- 什么是商业智能(BI),就看这篇文章足够了
- GTEST from ignorance to skillful use (1) GTEST installation
- [C language] deep understanding of symbols
- TCP三次握手,四次挥手,你真的了解吗?
- Open3D 曲面法向量计算
- 文件读取写入
- 服务线上治理
- HDU - 1078 fatmouse and cheese (memory search DP)
猜你喜欢
[C language] deep understanding of symbols
Analyzing the maker space contained in steam Education
[public class preview]: basis and practice of video quality evaluation
使用 BlocConsumer 同时构建响应式组件和监听状态
GTEST from ignorance to proficiency (3) what are test suite and test case
如何借助自动化工具落地DevOps
Application practice | Shuhai supply chain construction of data center based on Apache Doris
什么是商业智能(BI),就看这篇文章足够了
Nat. Commun.| 机器学习对可突变的治疗性抗体的亲和力和特异性进行共同优化
Keep on fighting! The city chain technology digital summit was grandly held in Chongqing
随机推荐
并列图的画法,多排多列
Lambdaquerywrapper usage
# 2156. 查找给定哈希值的子串-后序遍历
ACM Multimedia 2022 | 视觉语言预训练模型中社会偏见的反事实衡量和消除
Open3D 曲面法向量计算
Go语言循环语句(第10课中3)
QT—绘制其他问题
How to use concurrentlinkedqueue as a cache queue
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
EhLib 数据库记录的下拉选择
vim 从嫌弃到依赖(23)——最后的闲扯
WebGIS框架---kalrry
输入的查询SQL语句,是如何执行的?
Flutter WebView示例
面试题 01.01. 判定字符是否唯一
Which securities company is better to open an account? Is online account opening safe
一文掌握数仓中auto analyze的使用
Open3d surface normal vector calculation
Flink1.13 SQL basic syntax (I) DDL, DML
开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏