Cracking The Coding Interview 1.7
来源:互联网 发布:centos下安装hadoop 编辑:程序博客网 时间:2024/06/11 02:14
//Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.////这题原答案就是要两个buffer来记录出现0的位置。不知道有没可以不用buffer的方法。#include <iostream>using namespace std;void setZero(int **matrix, int m, int n){int *hang = new int[n];int *lie = new int[m];for (int i = 0; i< m; i++){for (int j=0; j <n;j++){if (matrix[i][j] == 0){hang[j] = 1;lie[i] = 1;}}}for (int i = 0; i<m; i++){for (int j = 0; j<n; j++){if (hang[j] ==1 || lie[i] ==1){matrix[i][j]=0;}}}}void display(int **matrix, int size, int n){for (int k = 0; k<size; k++)for (int t = 0; t<n; t++){cout<<matrix[k][t]<<" ";if (t==n-1){cout<<endl;}}}int main(){int m = 4,n = 5;int **p = new int *[m];for (int i=0;i<m;i++){p[i] = new int[n];}for (int i =0;i<m;i++){for (int j=0;j<n;j++){p[i][j] = 2*i+j; }}p[2][3] =0;display(p,m,n);cout<<endl;setZero(p,m,n);display(p,m,n);return 0;}
0 0
- Cracking The Coding Interview 1.7
- Cracking the Coding Interview
- 《Cracking the coding interview》
- Cracking The Coding Interview
- Cracking the coding interview
- Cracking the coding interview
- Cracking the coding interview--Q1
- Cracking the coding interview--Q17
- Cracking the coding interview--Q2
- Cracking the coding interview--Q3
- Cracking the coding interview--Q4
- Cracking the coding interview--Q5
- Cracking the coding interview--Q8
- Cracking the coding interview--Q9
- Cracking the coding interview 题目
- Cracking The Coding Interview 1.1
- Cracking The Coding Interview 1.2
- Cracking The Coding Interview 1.3
- java函数参数中:用参数值带回结果(类似传址)的方式及各种观点辨析
- Java中while循环用法
- objc 中的布尔值
- mvc easyui checkbox
- 从crtmpserver中看具体处理rtmp协议的流程
- Cracking The Coding Interview 1.7
- 黑马程序员_java高新技术_枚举与反射
- 最简单的方法,在index.jsp页面写一个JS跳转到你的action。
- 解决TIME_WAIT过多问题
- 你答应我今天飞来带着你迟来的真爱
- JS获取网页属性(转)
- 算法学习——LSC最大公有子序列 java实现
- ORA-600
- android 中ids.xml资源的使用