《Cracking the Coding Interview》——第1章:数组和字符串——题目7
2014-03-18 01:55
题目:给定一个MxN矩阵,如果某个元素为0,则将对应的整行和整列置为0。
解法:单独挑出一行和一列作为标记数组。因为某元素为0就全部置为0,所以不论A[i][j]为0中的j是几,第i行总会被置为0的。再用O(1)的额外空间去标记单独挑出的那一行一列是否包含0即可。要注意最后清零的顺序和范围不要错了。
代码:
// 1.7 Write an algorithm such that if an element in an MxN matrx is 0, its antire row and column are set to 0.
#include <cstdio>
#include <vector>
using namespace std; class Solution {
public:
void setMatrixZero(vector<vector<int> > &board) {
int m, n; m = (int)board.size();
if (m == ) {
return;
}
n = (int)board[].size();
if (n == ) {
return;
} int i, j;
bool row0_has_zero = false;
bool col0_has_zero = false;
for (i = ; i < m; ++i) {
if (board[i][] == ) {
col0_has_zero = true;
break;
}
}
for (j = ; j < n; ++j) {
if (board[][j] == ) {
row0_has_zero = true;
}
}
for (i = ; i < m; ++i) {
for (j = ; j < n; ++j) {
if (board[i][j] == ) {
board[i][] = ;
board[][j] = ;
}
}
}
for (i = ; i < m; ++i) {
if (board[i][] == ) {
for (j = ; j < n; ++j) {
board[i][j] = ;
}
}
}
for (j = ; j < n; ++j) {
if (board[][j] == ) {
for (i = ; i < m; ++i) {
board[i][j] = ;
}
}
}
if (row0_has_zero) {
for (j = ; j < n; ++j) {
board[][j] = ;
}
}
if (col0_has_zero) {
for (i = ; i < m; ++i) {
board[i][] = ;
}
}
}
}; int main()
{
vector<vector<int> > board;
int i, j;
int m, n;
Solution sol; while (scanf("%d%d", &m, &n) == && (m || n)) {
board.resize(m);
for (i = ; i < m; ++i) {
board[i].resize(n);
}
for (i = ; i < m; ++i) {
for (j = ; j < n; ++j) {
scanf("%d", &board[i][j]);
}
}
sol.setMatrixZero(board);
for (i = ; i < m; ++i) {
for (j = ; j < n; ++j) {
if (j > ) {
printf(" %d", board[i][j]);
} else {
printf("%d", board[i][j]);
}
}
printf("\n");
}
printf("\n");
} return ;
}
《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 co ...
- 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>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview 第一章
第一章:数组与字符串 1 数组与字符串 请实现一个算法,确定一个字符串的所有字符是否全都不同.这里我们要求不允许使用额外的存储结构. 给定一个string iniString,请返回一个bool值,T ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Cracking the Coding Interview 150题(一)
1.数组与字符串 1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.假设不允许使用额外的数据结构,又该如何处理? 1.2 用C或C++实现void reverse(char* str)函数, ...
- C语言 第七章 数组与字符串
一.数组 1.1.数组的概念 用来存储一组相同类型数据的数据结构.有点像班上放手机的手机袋,超市的储物柜. 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. ...
随机推荐
- 电路设计软件 电路模拟软件 sPlan , LTspice 等
电路设计/PCB绘制 立创EDA https://lceda.cn/ sPlan http://www.electronic-software-shop.com/splan-70.html?langu ...
- HDU(1166),线段树模板,单点更新,区间总和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 第一次做线段树,帆哥的一句话,我记下来了,其实,线段树就是一种处理数据查询和更新的手段. 然后, ...
- C# 命名空间与语句
C#采用命名空间(namespace)来组织程序.命名空间可以嵌套.using指示符可以用来简化命名空间类型的引用.using指示符有两种用法."using System;"语句可 ...
- 重定向跳出父Frame
当session过期后可以用过滤器来设置重定向页面 代码如下: public class ActionFilter extends HttpServlet implements Filter {pri ...
- iclr2015
http://www.iclr.cc/doku.php?id=iclr2015:main#accepted_papers iclr2015的accept papers,有些看过,有些没看明白,看来还是 ...
- django ORM单表操作
1.ORM介绍 ORM是“对象-关系-映射”的简称 映射关系: mysql---------Python 表名----------类名 字段----------属性 表记录--------实例化对象 ...
- BZOJ1901: Zju2112 Dynamic Rankings(整体二分 树状数组)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 9094 Solved: 3808[Submit][Status][Discuss] Descript ...
- Spring框架进阶3
Spring框架进阶3 测试spring_jdbc和spring对事务的管理 先配置相应的pom <?xml version="1.0" encoding="UTF ...
- JS - 属性描述符各配置的默认值的注意事项
通过字面量或者obj.x = 1;创建的属性 与 通过Object.defineProperty创建的属性,他们的属性描述符的默认值是不同的,前者都为true,后者都为false.
- 制作linux系统U盘并使用U盘安装CentOS7.6系统
目录 一.制作linux启动盘 1.1. 准备工作 1.2. 制作linux系统U盘 二.使用U盘安装Centos7.6 2.1. 使用U盘启动 2.2. 更改 ...