//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;
}

Cracking The Coding Interview 1.7的更多相关文章

  1. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  4. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  9. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. python中装饰器

    在介绍装饰器之前,要先了解装饰器的相关基础知识. 嵌套函数: 最后引入一个基本的装饰器的例子: __author__ = "YanFeixu" import time def ti ...

  2. WAMP环境配置

    mysql 5.7.12 php 7.1 apache 24 windows 10 64bit 下载好代码后, 1.首先把更目录的sql.sql导入到数据库2.打开application\config ...

  3. LeetCode--020--括号匹配

    题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空 ...

  4. 自定义Exception异常

    自定义异常构建 首先写一个自定义异常,继承Exception,代码如下 public class NoMappingParamString extends Exception { /*无参构造函数*/ ...

  5. apiCloud 选择图片,选择视频,压缩图片

    选择视频或图片,只需要一个 UIMediaScanner 模板就可以实现,点击链接查看api https://docs.apicloud.com/Client-API/UI-Layout/UIMedi ...

  6. 小程序分享转发功能实现demo

    /** * 用户点击右上角分享 */ onShareAppMessage: function() { //分享 console.log("分享") var that = this ...

  7. 数据库SQLserver(课本)

    一.SQL server的部署 1.数据库的基本概念 数据库通常是一个由行和列组成的二维表 数据表中的行通常叫做记录或元祖 数据表中的列通常叫做字段或属性 2.主键和外键 主键:定义主键可以保证数据的 ...

  8. poj-2369-置换

    Permutations We remind that the permutation of some final set is a one-to-one mapping of the set ont ...

  9. ASP.NET Core WebAPI 开发-新建WebAPI项目 转

    转 http://www.cnblogs.com/linezero/p/5497472.html ASP.NET Core WebAPI 开发-新建WebAPI项目   ASP.NET Core We ...

  10. laravel请求到响应的生命周期

    请求到响应的核个执行过程,主要可以归纳为四个阶段,即程序启动准备阶段.请求实例化阶段.请求处理阶段.响应发送和程序终止阶段. public\index.php中有这么一段代码 $app = requi ...