Cracking the coding interview--Q1.7
来源:互联网 发布:java一个汉字几个字符 编辑:程序博客网 时间:2024/06/04 19:49
原文转载自:http://hawstein.com/posts/1.7.html
题目
原文:
Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
译文:
写一个函数处理一个MxN的矩阵,如果矩阵中某个元素为0,那么把它所在的行和列都置为0.
解答
简单题。遍历一次矩阵,当遇到元素等于0时,记录下这个元素对应的行和列。 可以开一个行数组row和列数组col,当元素a[i][j]等于0时, 就把row[i]和col[j]置为true。第二次遍历矩阵时,当某个元素对应的行row[i] 或列col[j]被设置为true,说明该元素在需要被置0的行或列上,因此将它置0。
代码如下:
void zero(int **a, int m, int n){ bool row[m], col[n]; memset(row, false, sizeof(row)); memset(col, false, sizeof(col)); for(int i=0; i<m; ++i) for(int j=0; j<n; ++j) if(a[i][j] == 0){ row[i] = true; col[j] = true; } for(int i=0; i<m; ++i) for(int j=0; j<n; ++j) if(row[i] || col[j]) a[i][j] = 0;}
完整代码如下:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;void zero(int **a, int m, int n){ bool row[m], col[n]; memset(row, false, sizeof(row)); memset(col, false, sizeof(col)); for(int i=0; i<m; ++i) for(int j=0; j<n; ++j) if(a[i][j] == 0){ row[i] = true; col[j] = true; } for(int i=0; i<m; ++i) for(int j=0; j<n; ++j) if(row[i] || col[j]) a[i][j] = 0;}int main(){ freopen("1.7.in", "r", stdin); int m, n; cin>>m>>n; int **a; a = new int*[m]; for(int i=0; i<m; ++i) a[i] = new int[n]; for(int i=0; i<m; ++i) for(int j=0; j<n; ++j) cin>>a[i][j]; for(int i=0; i<m; ++i){ for(int j=0; j<n; ++j) cout<<a[i][j]<<" "; cout<<endl; } cout<<endl; zero(a, m, n); for(int i=0; i<m; ++i){ for(int j=0; j<n; ++j) cout<<a[i][j]<<" "; cout<<endl; } fclose(stdin); return 0;}
由于对文件的操作不熟悉,故将main()函数稍微修改了一下:
#include<iostream>#include<cstring>#include<cstdio>using namespace std;void zero(int **a, int m, int n){//bool row[m], col[n];bool *row = new bool[m];bool *col = new bool[n];memset(row, false, sizeof(row));memset(col, false, sizeof(col));for(int i = 0; i < m; i++)for(int j = 0; j < n; j++)if(a[i][j] == 0){row[i] = true;col[j] = true;}for(int i = 0; i < m; i++)for(int j = 0; j < n; j ++)if(row[i] || col[j])a[i][j] = 0;}int main(){const int M = 5, N = 4;int **a = new int*[M];for(int i = 0; i < M; i++)a[i] = new int[N];int num = 0;for(int i = 0; i < M; i++)for(int j = 0; j < N; j++){a[i][j] = num;num++;}for(int i = 0; i < M; i++){for(int j = 0; j < N; j++)cout<<" "<< a[i][j];cout<<endl;}zero(a, M, N);for(int i = 0; i < M; i++){for(int j = 0; j < N; j++)cout<<" "<< a[i][j];cout<<endl;}system("pause");return 0;}
0 0
- Cracking the coding interview--Q1.7
- Cracking the coding interview--Q1.7
- Cracking the coding interview--Q1.7
- Cracking the coding interview--Q1
- 【Cracking the coding interview】Q1.7(矩阵置0)
- Cracking The Coding Interview--Q1.1
- Cracking the coding interview Q1.1
- Cracking the coding interview--Q1.1
- Cracking the coding interview Q1.1
- Cracking the coding interview Q1.2
- Cracking the coding interview Q1.1
- Cracking the coding interview--Q1.8
- Cracking the Coding Interview Chap1 Q1.1
- Cracking the Coding Interview Chap1 Q1.2
- Cracking the Coding Interview Chap1 Q1.3
- Cracking the Coding Interview Chap1 Q1.4
- Cracking the Coding Interview Chap1 Q1.5
- Cracking the coding interview--Q1.1
- 《黑马程序员》基础加强---枚举类型
- 11.2处理周中的日期 (判断是否是周末)
- SWT学习笔记
- 如何处理“终端服务器超出了最大允许连接数”
- linux内核编码风范CodingStyle(中文版)
- Cracking the coding interview--Q1.7
- wxWidgets+CodeBlocks 官方HelloWorld程序
- 顺序表和单链表
- nginx main流程分析
- CSS介绍
- 17.3数组的排序,指针
- 模拟一个简单的spring(依赖注入)
- AsyncTask
- 卡特兰数(Catalan)应用:输出所有N对合法括号序列和输出所有已知进栈序列的合法出栈序列