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 ...
随机推荐
- Getting started with Processing 示例11-9 追随鼠标移动
程序片段 int num = 60; int[] x = new int[num]; int[] y = new int[num]; void setup(){ size(240,120); noSt ...
- socket 发送图片
using System;using System.Collections.Generic;using System.Text;using System.Net.Sockets;using Syste ...
- caffe 动态库 Release X64
Release X64平台 createdll.h#ifndef CREARDLL_H_#define CREARDLL_H_ extern "C" _declspec(dllex ...
- vue 基础(二)
Vue对象提供的属性功能 一.过滤器 过滤器,就是vue允许开发者自定义的文本格式化函数,可以使用在两个地方:输出内容和操作数据中. 1. 全局过滤器 Vue.filter 写在vm 对象外.必须要有 ...
- genymotio安装apk包提示 ...abi ...cpu
下载 Genymotion-ARM-Translation_v1.1 (1).zip 地址:http://qc1.androidfilehost.com/dl/Q-YDDKt4QaFNvKh62ppO ...
- CentOS中与网络相关的常用
CentOS中与网络相关的常用配置文件 1. 常见的网络配置文件 /etc/hosts 本地域名解析表,用于解析主机名.对应于win系统中的C:\Windows\System32\ ...
- python记录day24 模块的语法
使用模块的两种方式: import xxx from xxx import xxx 使用模块的优点: 1.实现代码的重用 2.可以把代码分类 一.import 导入模块的时候系统会做三件事 1.先去s ...
- DP 租用游艇
洛谷P1359租用游艇 分析:这个游艇我看到题目下意识的就想将dp数组设为dp[i][j]表示i到j之间的最短距离,但题目上要求的只是从起点到终点的距离,这样设只是自找麻烦. 直接设成dp[i]表示从 ...
- linux 查看日志命令
linux中命令cat.more.less均可用来查看文件内容, 区别:cat是一次性显示整个文件的内容,还可以将多个文件连接起来显示,它常与重定向符号配合使用,适用于文件内容少的情况:more和le ...
- set集合深浅拷贝以及知识补充
一. 对之前的知识点进行补充. 1. str中的join方法. 把列表转换成字符串 li = ["李嘉诚", "麻花藤", "黄海峰", & ...