Cracking The Coding Interview 1.7
//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的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- Java通过ftp上传文件
首先,pom.xml添加引用 <dependency> <groupId>commons-net</groupId> <artifactId>commo ...
- tornado web
tornado web frame: 非阻塞服务器,速度快,运用epoll 模板语言+render(),实现根据用户输入,自动渲染页面的动态效果. 在使用模板前需要在setting中设置模板路径: s ...
- You Don't Know JS: Async & Performance(第2章,Callbacks)
Chapter 2: Callbacks. Callbacks are by far the most common way that asynchrony in JS programs is exp ...
- git 基础知识
git 分布式版本控制系统 git三棵树: 工作目录 红色 等待添加到暂存区域 需执行git add filename 命令添加到暂存区 暂存区域 绿色 文件等待被提交 需执行 git commit ...
- 直播 APP 的直播实现流程
直播平台搭建所涉及的事项非常的广泛, 不仅需要直播源码. 直播系统开发. 后台服务 器.专门的运维人员等, 还需要技术团队切实的把控.下面, 小编就给大家确切的说下直播 平台搭建需要用到哪些步骤. 1 ...
- 理解JS中的this的指向
原文地址:https://www.cnblogs.com/pssp/p/5216085.html#1 首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到 ...
- Leetcode 116
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNod ...
- 数据结构与算法之PHP实现二叉树的遍历
一.二叉树的遍历 以某种特定顺序访问树中所有的节点称为树的遍历,遍历二叉树可分深度优先遍历和广度优先遍历. 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次.可以细分 ...
- PostgreSQL查看表大小的命令
SELECT table_name, pg_size_pretty(table_size) AS table_size, pg_size_pretty(indexes_size) AS indexes ...
- [hdu 6191] Query on A Tree
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...